如果在继承过程中只创建一个对象,那么 super 关键字引用的是什么?
If only one object is created during inheritance then what is super keyword referencing to?
在下面的代码中,super关键字引用的是什么?
class A {
int i1 = 10;
A() {
System.out.println("class A");
}
}
class B extends A {
int i2 = super.i1;
B() {
System.out.println("class B");
}
}
class C extends B {
int i3 = super.i2;
public static void main(String args[]) {
C c = new C();
System.out.println(c.i3);
}
}
如果没有创建 super class 的 objects/instances,那么 super 关键字引用的是什么,因为它是一个引用变量并且应该引用 super class 实例?
int i2 = super.i1; and int i2 = i1; both are same
因为 i1 从 parent class A 继承到 child class B
喜欢智者
int i3 = super.i2; and int i3 = i2; both are same
超仅指Parentclass。
当您创建 class C 的 object 时,将创建一个 object - C.
的一个实例
C 同时是 B 的实例和 A 的实例,也是 Object
由于C同时是B的实例和A的实例这就是为什么在构造函数中添加一个cal super()来调用parent class的构造函数C的构造函数调用B的构造函数和B的构造函数调用A 的构造函数
请注意 super
与 this
有点不同,因为 super
不是 主表达式 。事实上,super
根本不是一个表达式。所以说 super
本身 引用 是没有意义的。请注意,您不能这样做:
SomeType foo = super;
有意义的是这些(不是涉及 super
的所有内容的详尽列表):
- superclass 形式的构造函数调用
super(...);
- 表单的字段访问
super.identifier
super.identifier(...)
形式的方法调用
那些形式指的都是不同的东西,但是super
这个词本身没有单一的意思,除非你满足于“超class".
特别是在这个问题中,你问的是字段访问。这在语言规范的 15.11.2 节中有描述。
The form super.Identifier
refers to the field named Identifier
of the current object, but with the current object viewed as an instance of the superclass of the current class.
对于 i1
,由于 B
继承自 A
,因此 i1
将引用相同的字段,即使您将当前对象视为 B
,因此 class 您将当前对象视为什么并不重要。无论是 super.i1
,还是 this.i1
,或者只是 i1
,都没有区别。当您 在 B
中重新声明 i1
时,差异会更加明显:
class A {
int i1 = 10;
}
class B extends A {
int i1 = 20;
int i2 = super.i1;
}
i2
将获得值 10,而不是 20
,因为您将当前对象视为 A
。请注意,在这种情况下,B
的实例有两个名为 i1
的字段,一个继承自 A
,一个在 B
中声明。 “作为 A
”查看对象将使您看到从 A
继承的对象,否则它是隐藏的。
在下面的代码中,super关键字引用的是什么?
class A {
int i1 = 10;
A() {
System.out.println("class A");
}
}
class B extends A {
int i2 = super.i1;
B() {
System.out.println("class B");
}
}
class C extends B {
int i3 = super.i2;
public static void main(String args[]) {
C c = new C();
System.out.println(c.i3);
}
}
如果没有创建 super class 的 objects/instances,那么 super 关键字引用的是什么,因为它是一个引用变量并且应该引用 super class 实例?
int i2 = super.i1; and int i2 = i1; both are same
因为 i1 从 parent class A 继承到 child class B 喜欢智者
int i3 = super.i2; and int i3 = i2; both are same
超仅指Parentclass。 当您创建 class C 的 object 时,将创建一个 object - C.
的一个实例C 同时是 B 的实例和 A 的实例,也是 Object 由于C同时是B的实例和A的实例这就是为什么在构造函数中添加一个cal super()来调用parent class的构造函数C的构造函数调用B的构造函数和B的构造函数调用A 的构造函数
请注意 super
与 this
有点不同,因为 super
不是 主表达式 。事实上,super
根本不是一个表达式。所以说 super
本身 引用 是没有意义的。请注意,您不能这样做:
SomeType foo = super;
有意义的是这些(不是涉及 super
的所有内容的详尽列表):
- superclass 形式的构造函数调用
super(...);
- 表单的字段访问
super.identifier
super.identifier(...)
形式的方法调用
那些形式指的都是不同的东西,但是super
这个词本身没有单一的意思,除非你满足于“超class".
特别是在这个问题中,你问的是字段访问。这在语言规范的 15.11.2 节中有描述。
The form
super.Identifier
refers to the field namedIdentifier
of the current object, but with the current object viewed as an instance of the superclass of the current class.
对于 i1
,由于 B
继承自 A
,因此 i1
将引用相同的字段,即使您将当前对象视为 B
,因此 class 您将当前对象视为什么并不重要。无论是 super.i1
,还是 this.i1
,或者只是 i1
,都没有区别。当您 在 B
中重新声明 i1
时,差异会更加明显:
class A {
int i1 = 10;
}
class B extends A {
int i1 = 20;
int i2 = super.i1;
}
i2
将获得值 10,而不是 20
,因为您将当前对象视为 A
。请注意,在这种情况下,B
的实例有两个名为 i1
的字段,一个继承自 A
,一个在 B
中声明。 “作为 A
”查看对象将使您看到从 A
继承的对象,否则它是隐藏的。