Java中的构造函数链接 [英] Constructor Chaining in Java

查看:74
本文介绍了Java中的构造函数链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我稍微怀疑构造函数链接程序的输出,如下所示:

I hava slight doubt regarding the Output of the Program of Constructor Chaining I showed Below:

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& 20are aueueved?

The doubt is regarding the OutPut that how "1000", "10", "2000" & "20" are achiueved?

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

In the main Class We have created two Objects :

SpecialCube specialObj1 = new SpecialCube();
SpecialCube specialObj2 = new SpecialCube(10, 20);

首先使用无参数,第二个使用两个参数,第一个构造函数No Parameter只有两个值 this(10,10),而Two Parameters的值为

First with "No Parameters" and Second with "Two Parameters", The First Constructor Cube() with "No Parameter" has only two values this(10,10) and the one with "Two Parameters" has the values

Cube(int l, int b) 
    {this(l, b, 10);}

我不明白如何生成Below OutPuts。

I don't understand how the Below OutPuts are generated.

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

请任何人帮助我!

谢谢,
david

Thanks, david

推荐答案

我认为很明显。
specialObj1在默认构造函数中创建,调用1个参数构造函数,调用3个参数构造函数。每个调用发送10作为立方体维度的值。所以,音量是10 * 10 * 10 = 1000;

I think it is obvious. specialObj1 is created during default constructor that invokes 1 argument constructor, that invokes 3 argument constructor. Each invocation sends 10 as value of cube dimension. So, volume is 10*10*10 = 1000;

重量不是方法。它是在default和3-arg构造函数中初始化的字段。由于第一件事情,构造函数是调用这个(...)无关紧要的值分配给其他构造函数中的此变量。链中的第一个构造函数实际上覆盖所有先前设置的值。这是为什么第一个对象的weight = 10的原因。

weight is not method. It is field that is initialized in default and 3-arg constructor. Due to first thing the constructor does is call of this(...) it does not matter what value is assigned to this variable in other constructors. The first constructor in chain actually overrides all previously set values. This is a reason why weight = 10 for the first object.

第二个对象是使用参数10和20调用的2参数构造函数创建的,因此volume是10 * 20 * 10 = 2000.(第二个10在2args构造函数调用3args构造函数时设置。)
在第二个对象的情况下,权重由3-args构造函数设置,因为2-args构造函数不覆盖此值。

Second object is created using 2-arg constructor that is called with parameters 10 and 20, so volume is 10*20*10 = 2000. (second 10 is set when 2args constructor calls 3args constructor.) In case of second object the weight is set by 3-args constructor, because 2-args constructor does not override this value.

我希望这有助于。

这篇关于Java中的构造函数链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆