构造函数链接Java

Dav*_*own 3 java methods class

我对构造函数链接程序的输出有轻微疑问,我在下面展示:

class Cube {

    int length;
    int breadth;
    int height;
    public int getVolume() {
        return (length * breadth * height);
    }
    Cube() {
        this(10, 10);
        System.out.println("Finished with Default Constructor of Cube");
    }
    Cube(int l, int b) {
        this(l, b, 10);
        System.out.println("Finished with Parameterized Constructor having
                                    2 params of Cube");
    }
    Cube(int l, int b, int h) {
        length = l;
        breadth = b;
        height = h;
        System.out.println("Finished with Parameterized Constructor having
                                    3 params of Cube");
    }
}

public class SpecialCube extends Cube {

    int weight;
    SpecialCube() {
        super();
        weight = 10;
    }
    SpecialCube(int l, int b) {
        this(l, b, 10);
        System.out.println("Finished with Parameterized Constructor having
                                    2 params of SpecialCube");
    }
    SpecialCube(int l, int b, int h) {
        super(l, b, h);
        weight = 20;
        System.out.println("Finished with Parameterized Constructor having
                                    3 params of SpecialCube");
    }
    public static void main(String[] args) {
        SpecialCube specialObj1 = new SpecialCube();
        SpecialCube specialObj2 = new SpecialCube(10, 20);
        System.out.println("Volume of SpecialCube1 is : "
                + specialObj1.getVolume());
        System.out.println("Weight of SpecialCube1 is : "
                + specialObj1.weight);
        System.out.println("Volume of SpecialCube2 is : "
                + specialObj2.getVolume());
        System.out.println("Weight of SpecialCube2 is : "
                + specialObj2.weight);
    }
}
Run Code Online (Sandbox Code Playgroud)

OUTPUT:

Finished with Parameterized Constructor having 3 params of SpecialCube
Finished with Parameterized Constructor having 2 params of SpecialCube
Volume of SpecialCube1 is : 1000
Weight of SpecialCube1 is : 10
Volume of SpecialCube2 is : 2000
Weight of SpecialCube2 is : 20
Run Code Online (Sandbox Code Playgroud)

怀疑是关于OutPut,"1000","10","2000"和"20"是如何得到的?

在主类中我们创建了两个对象:

SpecialCube specialObj1 = new SpecialCube();
SpecialCube specialObj2 = new SpecialCube(10, 20);
Run Code Online (Sandbox Code Playgroud)

首先使用"无参数",第二个使用"两个参数",带有"无参数"的第一个构造函数立方体()只有两个值this(10,10),带有"两个参数"的值具有值

Cube(int l, int b) 
    {this(l, b, 10);}
Run Code Online (Sandbox Code Playgroud)

我不明白如何生成Below OutPuts.

Volume of SpecialCube1 is : 1000
Weight of SpecialCube1 is : 10
Volume of SpecialCube2 is : 2000
Weight of SpecialCube2 is : 20
Run Code Online (Sandbox Code Playgroud)

请有人帮帮我!

谢谢,大卫

And*_*s_D 7

当你调用时SpecialCube(),流程就像这样(伪代码):

SpecialCube()
  -> Cube()
     -> Cube(10,10)
        -> Cube(10,10,10)
           l:=10, b:=10, h:=10
           print message "Finished with Parameterized Constructor having 3 params of Cube"
        print message "Finished with Parameterized Constructor having 2 params of Cube"
     print message "Finished with Default Constructor of Cube"
  weight:=10
Run Code Online (Sandbox Code Playgroud)

最后你有一个用它构建的立方体 (l,b,h) = (10,10,10)