超类中的方法适用于java中的所有子类......如果子类有自己独立的方法,我怎样才能让它工作......
class Polymorphism3 {
public static void main(String[] args) {
// note how these are all considered objects of type Animal by Java
Animal[] a = new Animal[4];
a[0] = new Mouse();
a[1] = new Bird();
a[2] = new Horse();
a[3] = new Animal();
for (int i=0;i<4;i++) {
a[i].pullTail();
}
//...
class Animal {
public void pullTail() {
System.out.println("I don't know what to say.");
}
}
class Mouse extends Animal {
public void pullTail() {
System.out.println("fart");
}
}
class Bird extends Animal {
public void pullTail() {
System.out.println("tweet");
}
public void fly() {
System.out.println("flap");
}
}
class Horse extends Animal {
public void pullTail() {
System.out.println("neigh");
}
}