您好,登錄后才能下訂單哦!
java synthetic關(guān)鍵字。有synthetic標(biāo)記的field和method是class內(nèi)部使用的,正常的源代碼里不會(huì)出現(xiàn)synthetic field。小穎編譯工具用的就是jad.所有反編譯工具都不能保證完全正確地反編譯class。所以你不能要求太多。
下面我給大家介紹一下synthetic
下面的例子是最常見的synthetic field
Java代碼
class parent { public void foo() { } class inner { inner() { foo(); } } }
非static的inner class里面都會(huì)有一個(gè)this$0的字段保存它的父對(duì)象。編譯后的inner class 就像下面這樣:
Java代碼
class parent$inner{ synthetic parent this$0; parent$inner(parent this$0) { this.this$0 = this$0; this$0.foo(); } }
所有父對(duì)象的非私有成員都通過 this$0來訪問。
還有許多用到synthetic的地方。比如使用了assert 關(guān)鍵字的class會(huì)有一個(gè)synthetic static boolean $assertionsDisabled 字段
使用了assert的地方
assert condition;
在class里被編譯成
Java代碼
if(!$assertionsDisabled && !condition){ throw new AssertionError(); }
還有,在jvm里,所有class的私有成員都不允許在其他類里訪問,包括它的inner class。在java語言里inner class是可以訪問父類的私有成員的。在class里是用如下的方法實(shí)現(xiàn)的:
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.
注意上文的第一處黑體部分,一個(gè)類的復(fù)合屬性表示他支持嵌套的類或者接口。
注意上文的第二處黑體部分,說明符合這個(gè)概念就是OO思想中的類的復(fù)合,也就是只要含有其它類的引用即為復(fù)合。
總結(jié)
以上就是本文關(guān)于Java編程中的synthetic關(guān)鍵字的全部內(nèi)容,希望對(duì)大家能有所幫助。感謝大家對(duì)本站的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。