标签:
public interface Animal {
public abstract void eat();
}
public class Cat implements Animal{
public void eat(){
System.out.println("chi yu");
}
public void catchMouse(){
System.out.println("zhu lao shu");
}
}
public class Dog implements Animal{
public void eat(){
System.out.println("chi gu tou");
}
public void kanHouse(){
System.out.println("kan men");
}
}
public class Test {
public static void main(String[] args) {
Animal a = new Cat();
function(a);
}
public static void function(Animal a){
a.eat();
if(a instanceof Cat){
Cat c = (Cat)a;
c.catchMouse();
}else if(a instanceof Dog){
Dog d = (Dog)a;
d.kanHouse();
}
}
}
标签:
原文地址:http://www.cnblogs.com/wangqi327/p/4442603.html