client
--------------------------
public class Client{
public static void main(String[] args){
Person p = new Person();
Dog d = new Dog();
Cat c = new Cat();
p.feed(d);
p.feed(c);
}
}
person
----------------------------
public class Person{
//养猫
public void feed(Dog d){
d.eat();
}
// 养猫
public void feed(Cat c){
c.eat();
}
}
dog
--------------------------
public class Dog {
//Method
public void eat(){
System.out.println( "狗吃肉" );
}
}
cat
-----------------------------
public class Cat {
//Method
public void eat(){
System.out.println( "猫吃鱼" );
}
}
///////////////////////////////////////
难点
2 实现了低耦合 其实模块之间就是要求低耦合高内聚 这样有利于以后的开发 增加其它方法