2012年6月1日 星期五

Java Basic(61)-Simple Polymorphic Program


public class Fish extends Animal{
public void noise(){
System.out.println("Click Slurk");
}
}

public class Animal {

public void noise(){
System.out.println("Animal dont make noise");
}
}


public class Dog extends Animal{
public void noise(){
System.out.println("Ruff");
}

}

class apples{
public static void main(String[] args){
Animal[] theList = new Animal[2];
Dog d = new Dog();
Fish f = new Fish();
theList[0]=d;
theList[1]=f;
for(Animal x: theList){
x.noise();
}

}
}

這是運行時的結果:
Ruff 
Click Slurk 

沒有留言:

張貼留言