多态的应用 例子CODE01--head first



public class demo {
public static void main(String args[])
{
MyAnimalList list=new MyAnimalList();
Dog D1=new Dog();
Cat C1=new Cat();
list.add(D1);
list.add(C1);
//Animal wang=new Dog();
//wang.eat();
}
}


abstract class Animal{
boolean hunger;
String location;
abstract void eat();
}




class MyAnimalList{
private Animal[] animals=new Animal [5];//用来存对象的数组
private int nextIndex=0;
public void add(Animal a){
if(nextIndex<animals.length)
animals[nextIndex]=a;
System.out.println("Animal add at "+nextIndex);
nextIndex++;
}
}
阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭