public void eat()
{
System. out.println("eat");
}
}
class Cat extends Animal
{
public void show()
{
System.out.println("show cat");
}
public void eat()
{
System. out.println("eat 老鼠");
}
}
class Dog extends Animal
{
public void show()
{
System.out.println("show dog");
}
public void eat()
{
System. out.println("eat 骨头");
}
}
class Pig extends Animal
{
public void show()
{
System.out.println("show pig");
}
public void eat()
{
System. out.println("eat 饲料");
}
}
class AnimalTool
{
private AnimalTool(){}
/*
public static void printDog(Dog d)
{
d.show();
d.eat();
}
public static void printCat(Cat c)
{
c.show();
c.eat();
}
public static void printPig(Pig p)
{
p.show();
p.eat();
}
*/
public static void printAnimal(Animal a)
{
a.show();
a.eat();
}
}
class DuoTaiDemo5
{
public static void main(String[] args)
{
//一开始,养了一只狗
Dog d = new Dog();
d.show();
d.eat();
//接着又养了一只狗
Dog d2 = new Dog();
d2.show();
d2.eat();
//继续养狗
Dog d3 = new Dog();
d3.show();
d3.eat();
System.out.println("-----------------------");
//...养了很多只狗,我们就会发现一个问题,养的越多,代码的重复度越高。不好。
//如何改进呢?思考,他们调用的功能,仅仅是由于对象不一样。所以,我们能不能把
//每一个调用的整体封装一下呢,然后传递一个变化的对象即可呢?
//改进版代码
Dog d4 = new Dog();
Dog d5 = new Dog();
Dog d6 = new Dog();
/*
printDog(d4);
printDog(d5);
printDog(d6);
*/