Client
--------------------------------------------
public class Client{
public static void main(String[] args){
Person p = new Person();
//Animal a = new Animal();
Animal a = new Dog();//父类的引用指向子类的对象
p.feed(a);//方法的形参和实参也可以多态,形参声明的时候是父类的引用,但是实参传值的时候是子类对象的吧
Cat c = new Cat();
p.feed(c);
}
}
person
--------------------------
public void feed(Animal a){
a.eat();
}
}
animal
---------------------
public class Animal{
public void eat(){
}
}
dog
------------
public class Dog extends Animal{
//Method
public void eat(){
System.out.println( "狗吃肉" );
}
}
cat
------------------
public class Cat extends Animal{
//Method
public void eat(){
System.out.println( "猫吃鱼" );
}
}
难点 : 多态
animal 不再需要实例化 好处??????????????
完全解耦