首页 > Java > java教程 > 正文

Java中关于synthetic关键字的实例详解

黄舟
发布: 2017-09-15 10:09:05
原创
2024人浏览过

这篇文章主要介绍了浅谈java编程中的synthetic关键字的相关内容,包括其简单的介绍和实例,需要的朋友可以了解下。

java synthetic关键字。有synthetic标记的field和method是class内部使用的,正常的源代码里不会出现synthetic field。小颖编译工具用的就是jad.所有反编译工具都不能保证完全正确地反编译class。所以你不能要求太多。

下面我给大家介绍一下synthetic

下面的例子是最常见的synthetic field

Java代码

凡人网络购物系统jsp版(JspShop)
凡人网络购物系统jsp版(JspShop)

基于jsp+javabean+access(mysql)三层结构的动态购物网站,v1.2包含v1.0中未公开的数据库连接 的java源文件 一,网站前台功能: 产品二级分类展示:一级分类--二级分类--产品列表--详细介绍(名称,图片,市场价,会员价,是否推荐,功能介绍等) 产品搜索:关键字模糊搜索 定购产品:选择商品--确认定购--填写收货人信息--选择付款方式--订单号自动生成(限登录用户)

凡人网络购物系统jsp版(JspShop) 0
查看详情 凡人网络购物系统jsp版(JspShop)

立即学习Java免费学习笔记(深入)”;


class parent {
		public void foo() {
		}
		class inner {
			inner() {
				foo();
			}
		}
	}
登录后复制

非static的inner class里面都会有一个this$0的字段保存它的父对象。编译后的inner class 就像下面这样:
Java代码


class parent$inner{
	synthetic parent this$0;
	parent$inner(parent this$0)
	{
		this.this$0 = this$0;
		this$0.foo();
	}
}
登录后复制

所有父对象的非私有成员都通过 this$0来访问。
还有许多用到synthetic的地方。比如使用了assert 关键字的class会有一个synthetic static boolean $assertionsDisabled 字段
使用了assert的地方

assert condition;
在class里被编译成
Java代码


if(!$assertionsDisabled && !condition){
	throw new AssertionError();
	}
登录后复制

还有,在jvm里,所有class的私有成员都不允许在其他类里访问,包括它的inner class。在java语言里inner class是可以访问父类的私有成员的。在class里是用如下的方法实现的:

Java代码

立即学习Java免费学习笔记(深入)”;


class parent{
		private int value = 0;
		synthetic static int access$000(parent obj)
		{
			return value;
		}
	}
登录后复制

在inner class里通过access$000来访问value字段。
synthetic的概念

According to the JVM Spec: "A class member that does not appear in the source code must be marked using a Synthetic attribute." Also, "The Synthetic attribute was introduced in JDK release 1.1 to support nested classes and interfaces."

I know that nested classes are sometimes implemented using synthetic fields and synthetic contructors, e.g. an inner class may use a synthetic field to save a reference to its outer class instance, and it may generate a synthetic contructor to set that field correctly. I'm not sure if it Java still uses synthetic constructors or methods for this, but I'm pretty sure I did see them used in the past. I don't know why they might need synthetic classes here. On the other hand, something like RMI or java.lang.reflect.Proxy should probably create synthetic classes, since those classes don't actually appear in source code. I just ran a test where Proxy did not create a synthetic instance, but I believe that's probably a bug.

Hmm, we discussed this some time ago back here. It seems like Sun is just ignoring this synthetic attribute, for classes at least, and we should too.

注意上文的第一处黑体部分,一个类的复合属性表示他支持嵌套的类或者接口。

注意上文的第二处黑体部分,说明符合这个概念就是OO思想中的类的复合,也就是只要含有其它类的引用即为复合。

总结

以上就是Java中关于synthetic关键字的实例详解的详细内容,更多请关注php中文网其它相关文章!

相关标签:
java速学教程(入门到精通)
java速学教程(入门到精通)

java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号