简体   繁体   English

java构造函数访问(如果子类中有私有构造函数)

[英]java constructor accessing (if we have private constructor in child class)

I have a class A and class B which extends Class A.In class AI have a constructor "A" and in class BI have a constructor "B" and I have declare constructor "b" as private. 我有一个类A和一个扩展了类A的类B。在类AI中,有一个构造函数“ A”,在类BI中,有一个构造函数“ B”,并且我声明了构造函数“ b”为私有。 Will it be able to access its super class constructor "A"? 它能够访问其超类构造函数“ A”吗?

class A {
    public A(){
    }
}
class B extend A{
    private B(){
    }
}

Yes, B constructor will be able to call constructor of A. 是的,B构造函数将能够调用A的构造函数。

class A {
    public A(){
    System.out.println("Hello A constructor");
    }
}
class B extends A{
    private B(){
    super();
    System.out.println("Hello B constructor");
    }
    public static void main(String args[])
    {
    new B();
    }
}

Output - Hello A constructor Hello B constructor 输出-Hello A构造函数Hello B构造函数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM