我想知道Dynamic Binding
java的优势。
public class Animal {
public String type = "mammal";
public void show() {
System.out.println("The animal is a: " + type);
}
}
public class Dog extends Animal {
public String type;
public Dog(String type){
this.type = type;
}
public void show() {
System.out.println("The dog is a: " + type);
}
}
public static void main(String[] args) {
Animal doggie = new Dog("daschund");
doggie.show(); // "The dog is a: daschund" (dynamic binding)
System.out.println("The type is: " + doggie.type); //"The type is: mammal" (static binding)
}
我认为,这是一种继承策略。但是有些人称之为动态绑定。这就是为什么,我认为其中会有什么奇怪的地方。