淺談Java編程中的synthetic關(guān)鍵字
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)鍵字的全部?jī)?nèi)容,希望對(duì)大家能有所幫助。感謝大家對(duì)本站的支持。
相關(guān)文章
IDEA JetBrains Mono字體介紹和安裝教程(詳解)
這篇文章主要介紹了IDEA JetBrains Mono字體介紹和安裝教程,本給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03MyBatis3用log4j在控制臺(tái)輸出SQL的方法示例
本篇文章主要介紹了MyBatis3用log4j在控制臺(tái)輸出SQL的方法示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-01-01老生常談Java網(wǎng)絡(luò)編程TCP通信(必看篇)
下面小編就為大家?guī)硪黄仙U凧ava網(wǎng)絡(luò)編程TCP通信(必看篇)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-05-05Maven使用Nexus創(chuàng)建私服的實(shí)現(xiàn)
本文主要介紹了Maven使用Nexus創(chuàng)建私服的實(shí)現(xiàn),通過建立自己的私服,就可以降低中央倉(cāng)庫(kù)負(fù)荷、節(jié)省外網(wǎng)帶寬、加速M(fèi)aven構(gòu)建、自己部署構(gòu)件等,從而高效地使用Maven,感興趣的可以了解一下2024-04-04MybatisPlusException:Failed?to?process,Error?SQL異常報(bào)錯(cuò)的解決辦法
這篇文章主要給大家介紹了關(guān)于MybatisPlusException:Failed?to?process,Error?SQL異常報(bào)錯(cuò)的解決辦法,文中通過實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2023-03-03Spring Boot中@ConditionalOnProperty的使用方法
這篇文章主要給大家介紹了關(guān)于Spring Boot中@ConditionalOnProperty的使用方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者使用Spring Boot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12Java中多態(tài)的實(shí)現(xiàn)原理詳細(xì)解析
這篇文章主要介紹了Java中多態(tài)的實(shí)現(xiàn)原理詳細(xì)解析,多態(tài)是面向?qū)ο缶幊陶Z言的重要特性,它允許基類的指針或引用指向派生類的對(duì)象,而在具體訪問時(shí)實(shí)現(xiàn)方法的動(dòng)態(tài)綁定,需要的朋友可以參考下2024-01-01