简单构造函数练习 Java

huangapple go评论78阅读模式
英文:

simple constructor excersise Java

问题

以下是翻译好的内容:

我很难理解,为什么在下面这个例子中,类A的构造函数输出41,而类B的构造函数输出40。

class AK {
    AK(int i) {
        System.out.println("A类构造函数 " + i);
    }
    AK(){
        System.out.println("A类构造函数()");
    }
}

class BK extends AK {
    BK(int i) { 
        super(i+1); 
        System.out.println("B类构造函数 " + i);
    }
    
    BK(){
        System.out.println("B类构造函数()");
    }
}

class CK extends BK {
    CK(int i) {
        super(2 * i); 
        System.out.println("C类构造函数 " + i);
    }
}

class Main {
    public static void main (String args[]) {
        new CK(20);
    }
}

输出:
A类构造函数 41
B类构造函数 40
C类构造函数 20

英文:

I have troubles to understand, why class A constructor outputs 41, class B constructor outputs 40 in this case below

class AK {
    AK(int i) {
    	
    	System.out.println("Cstr of A " + i);
    }
    AK(){
    	
    	System.out.println("Cstr of A()");
    }
}
class BK extends AK {
    BK(int i) { 
    	super(i+1); 
    	System.out.println("Cstr of B " + i);
    }
    
    
    BK(){
        System.out.println("Cstr of B()");
    }
    
    
}
class CK extends BK {
    
    CK(int i) {
    	
    	super(2 * i); 
    	System.out.println("Cstr of C " + i);
    }
}
 
class Main {
    public static void main (String args[]) {
    	new CK(20);
    }
}

output:
Cstr of A 41
Cstr of B 40
Cstr of C 20

答案1

得分: 2

main() 函数中,您创建了一个名为 CK 的对象,并将整数 20 作为参数传递给它。CK 的构造函数调用了它的父类构造函数 BK,参数为 2 * parameter(= 40)。BK 的构造函数还调用了它的父类构造函数,参数为 parameter + 1(= 41)。

每个构造函数都会打印整数参数的值。之所以首先打印构造函数的结果,是因为它是第一个完成的构造函数,其他构造函数都在调用它。

调用带有参数 20 的 CK 构造函数 => i = 20
调用带有 i * 2 参数的 BK 构造函数 => i = 40
调用带有 i + 1 参数的 AK 构造函数 => i = 41

同时,i 从未被重新赋值,因此其值在其他构造函数中保持不变。

英文:

In main() you create an object CK with an integer 20 as parameter. The constructor of CK is calling its super constructor of BK with 2 * parameter (= 40). The constructor of BK is also calling its super constructor with parameter + 1 ( = 41).

And each constructor is printing its value of the integer parameter. The constructor is printing its result first because it is the first constructor that gets finished since the other constructors are calling it.

calling CK constructor with parameter 20 => i = 20
calling BK constructor with i * 2 => i = 40
calling AK constructor with i + 1 => i = 41

Also i is never reassigned so its value stays in the other constructor just as it is.

答案2

得分: 0

main()方法中,当您创建CK类的对象时,JRE会调用构造函数链,它将调用main() -> CK(20) -> BK(40) -> AK(41)。

当您调用CK(20)构造函数时,它会调用super(2i)=BK(int i)=BK(40)构造函数,并将值2i=2*20传递进去。
当调用带有值40的BK(int i)构造函数时,它会调用super(i+1)=AK(int i)=AK(41),传递的值为i+1=40+1。

由于在BK()中打印的是i的值而不是i+1,所以它打印的是40而不是41。

英文:

In main() method when you create object of CK, jre invokes constructor chaining which will call main() -> CK(20) -> BK(40)-> AK(41).

When you call CK(20) constructor, it calls super(2i)=BK(int i)=BK(40) constructor with value 2i=2*20.
When BK(int i) constructor is called with value 40, it calls super(i+1)=AK(int i)=AK(41) with value i+1=40+1.

Since you are printing value i in BK() and not i+1, it does print 40 instead of 41.

huangapple
  • 本文由 发表于 2020年10月5日 18:31:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/64206885.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定