2010-11-15 66 views
3

我哈瓦关于构造链接的程序的输出我发现下面轻微的疑问:构造链接在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 

的疑问是关于输出如何“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);} 

我不明白下面的输出是如何生成的。

Volume of SpecialCube1 is : 1000 
Weight of SpecialCube1 is : 10 
Volume of SpecialCube2 is : 2000 
Weight of SpecialCube2 is : 20 

请任何人都可以帮助我!

感谢, 大卫

+2

尝试调试代码,它可以跟踪流量 – 2010-11-15 10:08:26

+0

运算似乎是错误的。应该打印在立方体报表的最佳方式()构造函数第一个 – shaunak1111 2013-11-11 07:30:41

回答

3

我认为这是显而易见的。 specialObj1在缺省构造函数中创建,该构造函数调用1个参数构造函数,该构造函数调用3个参数构造函数。每个调用发送10作为多维数据集维度的值。因此,音量是10 * 10 * 10 = 1000;

重量不是方法。它是在默认和3-arg构造函数中初始化的字段。由于构造函数的第一件事是调用this(...),所以在其他构造函数中赋予此变量的值无关紧要。链中的第一个构造函数实际上覆盖了以前设置的所有值。这是第一个物体重量= 10的原因。

第二个目的是使用2参数的构造被称为具有参数10和20创建的,所以体积为10×20×10 = 2000。(第二10时2args构造函数调用3args构造方法设置。) 在的情况下,第二个对象的权重由3-args构造函数设置,因为2-args构造函数不会覆盖此值。

我希望这会有所帮助。

+1

无参数构造函数是一个不带任何参数的构造函数。当没有其他构造函数可用于该对象时,默认构造函数是编译器提供的对象的无参数构造函数克拉。在这个例子中没有默认的构造函数! – 2013-01-07 16:43:37

4

好了,让我们一个情况下,在一个时间。对于第一版本,构造链云:

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

的重量

如果这仍然不能解释你的一切,请问一个非常具体的问题 - 最好是一个的案件,说明哪一点你不明白。

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 

,并在结束时,你有一个立方体构建(l,b,h) = (10,10,10)

0

打破它:

SpecialCube1默认构造,所以调用默认构造函数SpecialCube(),这又调用super(),其中默认co nstructs Cube(),这将解释前两个。

其中SpecialCube2遵循SpecialCube正确的构造函数链。

0

这里这些输出中产生,因为

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 //在第一构造权重分配给10(即第一对象)

SpecialCube2的体积为:2000 //第二次getVolume函数获得来自specialcube的参数(3 argmted constr ie 10,20,10; SpecialCube2的

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; 

重量为:20 //在第二构造权重分配给20(即第一对象)

SpecialCube(INT升,INT B,INT 1H) { // L = 10,b = 20,H = 10

super(l, b, h); 
    System.out.println(l); 
System.out.println(b); 
System.out.println(h); 
    weight = 20; 
1
这里

执行流的下方,

步骤1:当“SpecialCube specialObj1 =新的特别立方体();”执行,默认构造函数“SpecialCube()”将被调用。

第2步:现在“超级()”将excuted叫“SpecialCube的”超班“魔方”。步骤3:现在将执行超类“cube”中的“this(10,10)”,它将从同一个类“cube”中调用2个参数的构造函数,即“Cube(int l,int b )“通过传递参数(l = 10,b = 10)。步骤4:现在“将在步骤#3中传递的实际参数执行”this(l,b,10)为“this(10,10,10)”,它将调用3参数化构造函数“Cube(int升,INT b,INT h)”时,与来自步骤#3的传递值,这将是象 “魔方(INT升= 10,INT b = 10,INT H = 10)”

步骤5:实例第1步:然后java会在构造函数“SpecialCube()”中将“Weight”变量赋值为10,这个变量的长度为10,宽度为10,高度为10,对于在步骤1创建的对象“specialObj1” ”。(参照步骤#1)

现在参见方法执行对于对象 “SpecialCube()”(我只是仅考虑 “specialObj1”

步骤7:的System.out.println(“SpecialCube1的体积是: “+ specialObj1.getVolume());,当这个语句执行时,java将调用超类”Cube's“”getVolume()“方法,因为子类”SpecialCube“通过关键字”extends“来引用,在你的子类中引用。

公共类SpecialCube延伸立方

步骤8:步骤1通5,已经分配instanace变量为“长度= 10,宽度= 1 0,heigth = 10“,你的音量变成”1000“。

步骤9: “的System.out.println(” SpecialCube1的重量是:“+ specialObj1.weight); - 此语句打印 “重量” 变量的值作为 “10”,因为步骤6#

希望,这将解释reason.Try相同的流为对象“specialObj2”。