//多态的应用
class Demo_Animal4 {
public static void main(String[] args) {
System.out.println("Hello World!");
Cat c=new Cat();
Dog d=new Dog();
funtion(d);
funtion(c);
}
public static void funtion(Animal c){
c.eat();
if(c instanceof Cat){
Cat a=(Cat)c;
a.catchMouse();
}
else if(c instanceof Dog){
Dog d=(Dog)c;
d.lookHome();
}
}
//public static void funtion(Dog d){
// d.eat();
//}
}
abstract class Animal {
public abstract void eat();
}
class Dog extends Animal {
public void eat(){
System.out.println("狗吃骨头");
}
public void lookHome(){
System.out.println("狗看家");
}
}
class Cat extends Animal {
public void eat(){
System.out.println("猫吃鱼");
}
public void catchMouse(){
System.out.println("抓老鼠");
}
}
|
|