class Animal
{
public void eat()
{
System.out.println("吃东西");
}
}
class Dog extends Animal
{
public void eat()
{
System.out.println("吃肉");
}
public void doWork()
{
System.out.println("看门");
}
}
class Cat extends Animal
{
public void eat()
{
System.out.println("吃鱼");
}
public void doWork()
{
System.out.println("捉老鼠");
}
}
class Person
{
public void feed( Animal a )
{
a.eat();
if (a instanceof Dog)
{
Dog b = (Dog)a;//引用类型转换
b.doWork();
}else if(a instanceof Cat)
{
Cat c = (Cat)a;
c.doWork();
}
}
}
class AnimalDemo
{
public static void main(String[] args)
{
Person s1 = new Person();
Dog a = new Dog();
Cat b = new Cat();
s1.feed(a);
s1.feed(b);
}
}
class Animal{ public void eat() { System.out.println("吃东西"); }}class Dog extends Animal{ public void eat() { System.out.println("吃肉"); } public void doWork() { System.out.println(...