public class Client{
public static void main(String[] args){
Person p = new Person();
Dog d = new Dog();
Cat c = new Cat();
//Animal a = new Animal();
//Animal a = new Dog();//父类的引用指向子类的对象
p.feed(d);//方法的形参和实参也可以多态,形参声明的时候是父类的引用,但是实参传值的时候是子类对象的吧
p.feed(c);
}
}
public class Person{
// 养狗
/*
public void feed(Dog d){
d.eat();
}
// 养猫
public void feed(Cat c){
c.eat();
}
*/
public class Dog extends Animal{
//Method
public void eat(){
System.out.println( "狗吃肉" );
}
}
public class Cat extends Animal{
//Method
public void eat(){
System.out.println( "猫吃鱼" );
}
}