您好,登錄后才能下訂單哦!
java synthetic關鍵字。有synthetic標記的field和method是class內部使用的,正常的源代碼里不會出現synthetic field。小穎編譯工具用的就是jad.所有反編譯工具都不能保證完全正確地反編譯class。所以你不能要求太多。
下面我給大家介紹一下synthetic
下面的例子是最常見的synthetic field
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代碼
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關鍵字的全部內容,希望對大家能有所幫助。感謝大家對本站的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。