我已经阅读过有关java中早期和晚期绑定的文章。我理解这个概念,但在某些情况下我无法区分它们。
代码№1:
class Human {
public void walk() {
System.out.println("Human walks");
}
}
class Boy extends Human {
public void walk() {
System.out.println("Boy walks");
}
public static void main(String args[]) {
Boy obj = new Boy();
obj.walk();
}
}
代码№2:
class Human {
public void walk() {
System.out.println("Human walks");
}
}
public class Boy extends Human {
public void walk() {
System.out.println("Boy walks");
}
public static void test(Boy obj) {
obj.walk();
}
public static void main(String args[]) {
Boy obj = new Boy();
test(obj);
}
}
代码№3:
class Human {
public void walk() {
System.out.println("Human walks");
}
}
class OldBoy extends Boy {
public void walk() {
System.out.println("OldBoy walks");
}
}
public class Boy extends Human {
public void walk() {
System.out.println("Boy walks");
}
public static void test(Boy obj) {
obj.walk();
}
public static void main(String args[]) {
Boy obj = new Boy();
test(obj);
}
}
什么类型的绑定(迟到或早期)在代码№1,代码№2和代码№3中调用obj.walk()?为什么?