超级关键字歧义
Super keyword ambiguity
下面的程序
class Feline {
public String type = "f ";
public Feline() {
System.out.print("feline ");
}
}
public class Cougar extends Feline {
public Cougar() {
System.out.print("cougar ");
}
void go() {
type = "d ";
System.out.print(this.type + super.type);
}
public static void main(String[] args) {
new Cougar().go();
}
}
产生以下输出。
feline cougar d d
我无法理解为什么 super.type
在生成输出时没有从超类中获取值并打印 f
而不是打印本地值 d
.有人可以在这里提供一些帮助吗?
是相反的,现在你的type
合并在一起,当你改变type
的值时,它会覆盖super.type
。
这里有一个简短的例子作为解释:
void go() {
System.out.print(this.type);
type = "d ";
System.out.print(this.type + super.type);
}
输出:
f d d
其实就是把super.type
的值f
改成d
试图以简单的方式解释 classes 和对象:
将 classes 视为对象的原型或模型。
当您创建 class B extending a class A (other) 时,这意味着模型 B 具有 A 的所有行为以及一些额外的行为。此外,B 还可以覆盖(替代)A 的一些行为。
到目前为止,我们还没有谈及对象。
现在,在 main()
中,您正在创建类型 B 的对象(也称为实例)。
您只有一个对象。因此,你只有一个状态。它不是每个 class 的一个状态,因为状态与实例相关。
这意味着,您代码中的 type
来自这个单个对象。无论您是从 this
还是 super
.
引用它都没有关系
如果您覆盖某个方法,然后调用这些方法,则可以在 this
和 super
之间实现不同的行为。
试一试:
在 Feline class 中创建方法 sayHello()
,打印一些文本。然后,在 Cougar class 中创建另一个 sayHello()
,使用不同的文本。
然后,在 go()
内调用:
this.sayHello();
super.sayHello();
因为除非您将每个 type
属性设置为 private
,否则 type
是可见的并且 相同 在父 class(Feline
):你不改Cougar
'type
,而是改Feline
'type
,因为是继承的。
这个例子应该更清楚:
class A {
private String type;
public String getType() {return type;}
}
class B extends A {
private int type;
public void go() {
System.out.println("this.type: " + this.type + ", super.type: "+ super.getType());
}
public static void main(String[] args) {
new B().go(); // prints: this.type: 0, super.type: null
}
}
下面的程序
class Feline {
public String type = "f ";
public Feline() {
System.out.print("feline ");
}
}
public class Cougar extends Feline {
public Cougar() {
System.out.print("cougar ");
}
void go() {
type = "d ";
System.out.print(this.type + super.type);
}
public static void main(String[] args) {
new Cougar().go();
}
}
产生以下输出。
feline cougar d d
我无法理解为什么 super.type
在生成输出时没有从超类中获取值并打印 f
而不是打印本地值 d
.有人可以在这里提供一些帮助吗?
是相反的,现在你的type
合并在一起,当你改变type
的值时,它会覆盖super.type
。
这里有一个简短的例子作为解释:
void go() {
System.out.print(this.type);
type = "d ";
System.out.print(this.type + super.type);
}
输出:
f d d
其实就是把super.type
的值f
改成d
试图以简单的方式解释 classes 和对象:
将 classes 视为对象的原型或模型。
当您创建 class B extending a class A (other) 时,这意味着模型 B 具有 A 的所有行为以及一些额外的行为。此外,B 还可以覆盖(替代)A 的一些行为。
到目前为止,我们还没有谈及对象。
现在,在 main()
中,您正在创建类型 B 的对象(也称为实例)。
您只有一个对象。因此,你只有一个状态。它不是每个 class 的一个状态,因为状态与实例相关。
这意味着,您代码中的 type
来自这个单个对象。无论您是从 this
还是 super
.
如果您覆盖某个方法,然后调用这些方法,则可以在 this
和 super
之间实现不同的行为。
试一试:
在 Feline class 中创建方法 sayHello()
,打印一些文本。然后,在 Cougar class 中创建另一个 sayHello()
,使用不同的文本。
然后,在 go()
内调用:
this.sayHello();
super.sayHello();
因为除非您将每个 type
属性设置为 private
,否则 type
是可见的并且 相同 在父 class(Feline
):你不改Cougar
'type
,而是改Feline
'type
,因为是继承的。
这个例子应该更清楚:
class A {
private String type;
public String getType() {return type;}
}
class B extends A {
private int type;
public void go() {
System.out.println("this.type: " + this.type + ", super.type: "+ super.getType());
}
public static void main(String[] args) {
new B().go(); // prints: this.type: 0, super.type: null
}
}