构造函数在Java中链接
我对构造函数链接程序的输出有轻微疑问,我在下面展示:
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); } }
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
怀疑是关于OutPut,“1000”,“10”,“2000”和“20”是如何得到的?
在主类中我们创建了两个对象:
SpecialCube specialObj1 = new SpecialCube(); SpecialCube specialObj2 = new SpecialCube(10, 20);
首先使用“无参数”,第二个使用“两个参数”,带有“无参数”的第一个构造函数立方体()只有两个值this(10,10)
,带有“两个参数”的值具有值
Cube(int l, int b) {this(l, b, 10);}
我不明白如何生成Below OutPuts。
Volume of SpecialCube1 is : 1000 Weight of SpecialCube1 is : 10 Volume of SpecialCube2 is : 2000 Weight of SpecialCube2 is : 20
请有人帮帮我!
谢谢,大卫
我认为很明显。 specialObj1是在默认构造函数中创建的,它调用1个参数构造函数,调用3个参数构造函数。 每次调用都将10作为多维数据集维度的值发送。 所以,体积是10 * 10 * 10 = 1000;
重量不是方法。 它是默认初始化的字段和3-arg构造函数。 由于构造函数所做的第一件事就是调用它(…),在其他构造函数中为这个变量赋值是什么并不重要。 链中的第一个构造函数实际上覆盖了所有先前设置的值。 这是第一个对象的权重= 10的原因。
第二个对象是使用参数10和20调用的2-arg构造函数创建的,因此volume是10 * 20 * 10 = 2000.(当2args构造函数调用3args构造函数时,设置第二个。)如果是第二个对象,则权重为由3-args构造函数设置,因为2-args构造函数不会覆盖此值。
我希望这有帮助。
当您调用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
最后你有一个用(l,b,h) = (10,10,10)
构造的立方体
好吧,让我们一次拿一个案例。 对于第一个版本,构造函数链是:
SpecialCube() Cube() Cube(10, 10) Cube(10, 10, 10)
因此,立方体最终的体积为1000,默认权重为10(在SpecialCube
参数构造函数中指定)。
在第二种情况下,构造函数链如下所示:
SpecialCube(10, 20) SpecialCube(10, 20, 10) Cube(10, 20, 10)
并且通过SpecialCube(int l, int b, int h)
参数构造函数将权重设置为20 – 因此我们的体积为2000,权重为20。
如果仍然没有向你解释一切,请提出一个非常具体的问题 – 最好是关于其中一个案例,说明你不明白哪一点。
下面是执行流程,
第1步:当“SpecialCube specialObj1 = new SpecialCube();”时 执行后,将调用默认构造函数“SpecialCube()”。
第2步:现在“Super()”将被称为“SpecialCube”的超级“Cube”。
第3步:现在将执行超级“立方体”中的“this(10,10)”,它将通过传递从同一个类“cube”调用2个参数的构造函数,即“Cube(int l,int b)”参数(L = 10,b = 10)。
第4步:现在“这个(l,b,10)将执行,步骤#3中传递的实际参数是”this(10,10,10)“,它将调用3参数化构造函数”Cube(int l,int b) ,int h)“,使用来自步骤#3的值,它将类似于”Cube(int l = 10,int b = 10,int h = 10)“。
步骤5:对于在步骤#1中创建的对象“specialObj1”,实例变量如length = 10,breadth = 10,heigth = 10。
步骤6:然后java将在构造函数“SpecialCube()”中将“权重”变量指定为10。 (参考步骤#1)
现在看到对象“SpecialCube()”的方法执行(我只考虑“specialObj1”)。
步骤7:System.out.println(“SpecialCube1的卷是:”+ specialObj1.getVolume()); ,当执行此语句时,java将调用超类“Cube”“getVolume()”方法,因为子类“SpecialCube”inheritance其关键字“extends”,请参阅下面的子类。
公共类SpecialCube扩展了Cube
步骤8:步骤1至5,已经分配的instanace变量为“length = 10,breadth = 10,heigth = 10”,你的获取量为“1000”。
步骤9:“System.out.println(”SpecialCube1的权重为:“+ specialObj1.weight); – 由于步骤#6,此语句将”weight“变量值打印为”10“。
希望,它会解释原因。为对象“specialObj2”尝试相同的流程。
分解:
SpecialCube1
是默认构造的,因此调用默认构造函数SpecialCube()
,后者又调用super()
,默认构造Cube()
,它将解释前两个。
其中, SpecialCube2
遵循SpecialCube2
的正确构造函数链。
这里产生这些输出是因为
SpecialCube1的体积是:1000 //当第一次调用getVolume函数时,它获得10,10,10,因为参数观察下面的代码,其中l,b,h值分配给长度,宽度,高度
Cube(int l, int b, int h) { //l=10,b=10,h=10 System.out.println(l); System.out.println(b); System.out.println(h); length = l; breadth = b; height = h;
SpecialCube1的重量为:10 //在第一个constr权重中分配给10(即第一个对象)
SpecialCube2的卷是:2000 //第二次getVolume函数从specialcube获取参数(3 argmted constr即10,20,10;
SpecialCube(int l, int b, int h) { //l=10,b=20,h=10 super(l, b, h); System.out.println(l); System.out.println(b); System.out.println(h); weight = 20;
SpecialCube2的重量为:20 //在第二个constr权重分配为20(即第一个对象)
SpecialCube(int l,int b,int h){// l = 10,b = 20,h = 10
super(l, b, h); System.out.println(l); System.out.println(b); System.out.println(h); weight = 20;