我正在学习,我遇到了这段代码:
class Shoe{
public Shoe(){
this("This is a shoe");
System.out.println("Base Class");
}
public Shoe(String s){
System.out.println(s);
}
}
class TennisShoe extends Shoe{
public TennisShoe(){
this("This is a Tennis Shoe");
System.out.println("Derived Class");
}
public TennisShoe(String s) {
super("Exam 1");
System.out.println(s);
}
}
class WhiteTennisShoe extends TennisShoe{
public WhiteTennisShoe(String s){
System.out.println(s);
}
}
class Test{
public static void main(String args[]) {
new WhiteTennisShoe ("A white tennis shoe is created");
}
}
我认为输出将是:
A white tennis shoe is created
因为只调用了子类 WhiteTennisShoe 的构造函数。然而,真正的输出是完全不同的:
Exam 1
This is a Tennis Shoe
Derived Class
A white tennis shoe is created
这是因为动态和静态绑定吗?还是纯java继承?我真的很困惑。几天来,我一直在寻找关于静态和动态绑定的一个很好的解释,但我没有找到。