public class B extends A{
// code goes here
}
public class C extends B{
public void method1(){
C c = new C();
}
}
由于 C 扩展了 B ,而 B 又扩展了 A ,所以当我创建 C 的对象时,将在 JVM 中创建多少个对象。
public class B extends A{
// code goes here
}
public class C extends B{
public void method1(){
C c = new C();
}
}
由于 C 扩展了 B ,而 B 又扩展了 A ,所以当我创建 C 的对象时,将在 JVM 中创建多少个对象。
一,C
类型。打算在这里添加一些东西,让答案超过 30 个符号......
One object will be directly created. I say "directly" because initialization expressions or constructor code could create other objects using "new".
Creating that one C object will cause four constructor calls. Immediately before the body of C's constructor, there is an explicit or implicit call to a B constructor. Similarly, B's constructor calls an A constructor, which calls the Object constructor.
The object is a C, and is a B, and is an A, and is an Object, and by the end of the process will have been initialized as each of them.
For full gory details on this process, see the JLS.
答案是 1。调用new C()
创建了一个确实扩展了自身的对象B
,A
因此您可以使用从 B 和 A 继承的功能。