//多态的案例
class Demo_DuoTai {
public static void main(String[] args) {
System.out.println("Hello World!");
/*
c.eat();
c.cathMouse();
Dog d=new Dog();
d.eat();
d.lookHome();*/
/*Animal c=new Cat();
funtion(c);
Animal d=new Dog();
funtion(d);
Animal p=new Pig();
funtion(p);*/
//funtion(new Cat());
//funtion(new Dog());
//funtion(new Pig());
Animal a=new Cat();
funtion(a);
//Cat a=(Cat)c;
//a.cathMouse();
//Cat c=(Cat) a;
//c.cathMouse();
}public static void funtion(Animal c){
c.eat();
Cat a=(Cat) c;
a.cathMouse();
//c.cathMouse();
}
/*public static void funtion(Dog d){
d.eat();
d.lookHome();
}
public static void funtion(Pig p){
p.eat();
p.gongDi();
}*/
}
abstract class Animal {
abstract public void eat();
}
class Cat extends Animal{
public void eat(){
System.out.println("喵吃鱼");
}
public void cathMouse(){
System.out.println("抓老鼠");
}
}
class Dog extends Animal {
public void eat(){
System.out.println(" kengutou");
}
public void lookHome(){
System.out.println("看家");
}
}
class Pig extends Animal{
public void eat(){
System.out.println("饲料");
}
public void gongDi(){
System.out.println("拱地");
}
}
|
|