一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - 淺談Java編程中的synthetic關(guān)鍵字

淺談Java編程中的synthetic關(guān)鍵字

2021-01-03 15:22mengwei Java教程

這篇文章主要介紹了淺談Java編程中的synthetic關(guān)鍵字的相關(guān)內(nèi)容,包括其簡(jiǎn)單的介紹和實(shí)例,需要的朋友可以了解下。

java synthetic關(guān)鍵字。有synthetic標(biāo)記的field和method是class內(nèi)部使用的,正常的源代碼里不會(huì)出現(xiàn)synthetic field。小穎編譯工具用的就是jad.所有反編譯工具都不能保證完全正確地反編譯class。所以你不能要求太多。

下面我給大家介紹一下synthetic

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

Java代碼

?
1
2
3
4
5
6
7
8
9
class parent {
        public void foo() {
        }
        class inner {
            inner() {
                foo();
            }
        }
    }

非static的inner class里面都會(huì)有一個(gè)this$0的字段保存它的父對(duì)象。編譯后的inner class 就像下面這樣:
Java代碼

?
1
2
3
4
5
6
7
8
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代碼

?
1
2
3
if(!$assertionsDisabled && !condition){
    throw new AssertionError();
    }

還有,在jvm里,所有class的私有成員都不允許在其他類里訪問,包括它的inner class。在java語言里inner class是可以訪問父類的私有成員的。在class里是用如下的方法實(shí)現(xiàn)的:

Java代碼

?
1
2
3
4
5
6
7
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ì)本站的支持。

原文鏈接:https://www.2cto.com/kf/201703/618626.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 动漫白丝袜美女羞羞 | yellow视频在线观看 | 夫妻性生活一级黄色片 | 男人香蕉好大好爽视频 | 亚洲精品国精品久久99热 | 国产成人精品免费 | 日韩欧美一区二区三区免费看 | av中文字幕网免费观看 | 国产精品麻豆久久99 | 亚洲国产精品嫩草影院久久 | 逼逼日| 日产精品一二三四区国产 | 女教师雪白老汉 | 国产成人夜色91 | 亚洲精品在线免费 | 古代色翁荡熄 | 成人在线av视频 | 贰佰麻豆剧果冻传媒一二三区 | 国产成人激情视频 | 7788av| 特黄a大片免费视频 | 国产91在线精品 | 欧美日韩专区国产精品 | 国产无限免费观看黄网站 | 操弄哥哥的108种姿势 | 日韩一区国产二区欧美三 | 操丝袜秘书 | 亚洲第一网站免费视频 | 欧美日韩一区二区三区久久 | 紧身牛仔裤美女被啪啪久久网 | 色综合久久天天综合 | 草莓丝瓜芭乐樱桃榴莲色多黄 | 草女人逼| 修修视频在线观看 | 国内精品视频九九九九 | 色综合久久丁香婷婷 | 垫底辣妹免费观看完整版 | 亚洲欧洲日产v特级毛片 | 白俄罗斯bbbsss | bt天堂午夜国产精品 | 香蕉在线精品一区二区 |