java繼承多個父類,java 父類私有成員_java父類私有成員

 2023-11-18 阅读 23 评论 0

摘要:public class UnSafeSequence {public class TestMath{private TestMath(){java繼承多個父類?System.out.println("父類實例化");}}public class TestMath1 extends TestMath{私有成員不能被繼承,public TestMath1(){System.out.println("子類實例化");}}ja

public class UnSafeSequence {

public class TestMath{

private TestMath(){

java繼承多個父類?System.out.println("父類實例化");

}

}

public class TestMath1 extends TestMath{

私有成員不能被繼承,public TestMath1(){

System.out.println("子類實例化");

}

}

java公有類和私有類、/**

* @param args

*/

public static void main(String[] args) {

父類的私有成員會被繼承碼。System.out.println(new UnSafeSequence().new TestMath1());

}

}

java6語言規范中關于private修飾符的描述,頂級類及內部類的定義

java私有類、6.6.1 if the member or constructor is declared private, then access is permitted if and only if it occurs within the body of the top level class (§7.6) that encloses the declaration of the member or constructor.

如果一個類的成員或構造器聲明為private的,那么只有聲明這個成員或構造器的頂級類才有權訪問(當然聲明這個成員和構造函數的類也是可以訪問的)

8.A top level class is a class that is not a nested class.A nested class is any class whose declaration occurs within the body of another class or interface. A top level class is a class that is not a nested class.頂級類不是一個嵌套類(內部類),嵌套類(內部類)是申明在其他類或接口中的類鑒于以上的規定描述,那么外部類中可以訪問構造器標示為private的TestMath內部類。TestMath1同樣是一個內部類,其繼承了另一個內部類TestMath,因為一個內部類依賴外部類實例對象而存在,會隱式的關聯一個外部類實例

所以

java繼承私有屬性。public class TestMath1 extends TestMath{

public TestMath1(){

System.out.println("子類實例化");

}

}

可以寫成

public class TestMath1 extends UnSafeSequence.TestMath{

public TestMath1(){

UnSafeSequence.this.super();

System.out.println("子類實例化");

}

}

這樣就可以解釋一個內部類的子類為什么可以訪問其父類的私有的構造函數了

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/1/178037.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息