我不确定我的问题措辞是否正确,但我对这些代码行感到困惑。
public class First {
public String name() {
return "First";
}
}
public class Second extends First {
public void whoRules() {
System.out.print(super.name() + " rules");
System.out.println(" but " + name() + " is even better");
}
public String name() {
return "Second";
}
}
public class Third extends Second {
public String name() {
return "Third";
}
}
Second varSecond = new Second();
Third varThird = new Third();
varSecond.whoRules();
varThird.whoRules();
执行上述操作时,它会打印出
First rules but second is even better
First rules but third is even better
为什么不是:
First rules but second is even better
Second rules but third is even better
一个子类可以成为另一个类的超类吗?还是只能有一个(超类)?(使用上面的示例代码)我知道这First
是一个超类Second
,但它是Second
一个超类Third
?或者是First
超类Third
?
如果有 10 个类相互扩展(第二个扩展第一个,第三个扩展第二个等)所有这些类的超类是First
吗?)