gpt4 book ai didi

java - 与私有(private)构造函数混淆

转载 作者:行者123 更新时间:2023-12-01 18:30:21 26 4
gpt4 key购买 nike

在java中研究,如果一个类有私有(private)构造函数,它不能被继承为super(),不能被调用,那么为什么在内部类的情况下这段代码可以编译:

public class TestMe2 { 
class Singleton {
private Singleton() {
}
}
class BB extends Singleton {
}

public void callMe(){
Singleton sing=new BB();
}
}

最佳答案

因为一个公共(public)外部类的2个内部类可以访问它们各自的私有(private)成员。

引自the JLS :

A private class member or constructor is accessible only within the body of the top level class (§7.6) that encloses the declaration of the member or constructor.

关于java - 与私有(private)构造函数混淆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24495100/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com