abstract class Animal
{
public abstract void eat();
}
class Cat extends Animal
{
public void eat()
{
System.out.println("鱼");
}
public void catchMouse()
{
System.out.println("抓老鼠");
}
}
class Dog extends Animal
{
public void eat()
{
System.out.println("骨头");
}
public void kanJia()
{
System.out.println("咬人");
}
}
class DuoTaiDemo2
{
public static void main(String[] args)
{
//Animal a = new Cat();
//Cat c = (Cat)a;
//function(a);
//c.catchMouse();
function(new Cat());
function(new Dog());
}
public static void function(Animal a)
{
a.eat();//为什么可以直接用a呢,也没定义a啊,为什么编译通过不提醒找不到对象呢
if (a instanceof Cat)
{
Cat c = (Cat)a;
c.catchMouse();
}
else if (a instanceof Dog)
{
Dog d = (Dog)a;
d.kanJia();
}