Constructor interview questions (implementation of parameter construction)

Understand the following three conclusions

class A {
    
    
    private static int numA;
    private int numA2;

    static {
    
    
        System.out.println("A的静态字段 : " + numA);
        System.out.println("A的静态代码块");
    }

    {
    
    
        System.out.println("A的成员变量  : " + numA2);
        System.out.println("A的非静态代码块");
    }

    public A() {
    
    
        System.out.println("A的构造器");
    }

    public A(int n) {
    
    
        System.out.println("A的有参构造");
        this.numA2 = n;
    }
}

class B extends A {
    
    
    private static int numB;
    private int numB2;

    static {
    
    
        System.out.println("B的静态字段 : " + numB);
        System.out.println("B的静态代码块");
    }

    {
    
    
        System.out.println("B的成员变量 : " + numB2);
        System.out.println("B的非静态代码块");
    }

    public B() {
    
    
        System.out.println("B的构造器");
    }

    public B(int n) {
    
    
        System.out.println("B的有参构造");
        this.numB2 = n;
    }
}

public class Test4{
    
    
    public static void main(String[] args) {
    
    
        B anotherB = new B(1);// 思考有参构造的输出结果
    }
}

When the parameterized construct of subclass B is called, it is certain that the constructor of the parent class is executed first, but which constructor is executed?

The execution result is as follows:
Insert picture description here
As you can see in the execution result, the parent class A still executes the no-parameter construction.

In other words,If the parent class constructor is not explicitly specified in the subclass constructor, the no-parameter construction of the parent class will be executed by default [Conclusion 1]
in other words,If the parent class constructor is explicitly specified in the subclass constructor (that is, super(n); is added to the first line), then the parameterized construction of the parent class will be executed [Conclusion 2]

我们证明如下:(为了简化,我们注释掉静态成员,从而更好的专注于问题)
当我们在子类中显式地指定有参构造后(即在子类有参构造的第一行加上super(n);),则输出结果如下

public B(int n) {
    
    
        //super();
        super(n);
        System.out.println("B的有参构造");
        this.numB2 = n;
}

Insert picture description here

All constructors in the subclass default [default: no parent class constructor is explicitly specified] will access the constructor with empty parameters in the parent class [Conclusion 3]

证明如下:

我们注释掉父类的无参构造,但是保留子类的无参构造,结果发生编译错误
原因:所有的构造器包括子类中的无参构造,都会默认访问父类中的无参构造,这里子类中存在的无参构造器就没有显式指定父类构造器(子类中的有参构造器已经在第一行指定了父类构造器),但是父类中没有无参构造。

解决办法:我们可以注释掉子类中的无参构造,这样,子类中的所有构造器都显式地指定了父类构造器。

Guess you like

Origin blog.csdn.net/AC_872767407/article/details/113575536