class Animal {
public void sound(){}
}
class Dog extends Animal{
public void sound(){
System.out.print("汪汪");
}
}
class Cat extends Animal{
public void sound(){
System.out.print("喵喵");
}
}
public class Test{
public static void main(String[] args) {
Animal c = new Cat();
Animal d = new Dog();
System.out.print("tom");
c.sound();
System.out.print("旺财");
d.sound();
}
}
就是这种你在增加不同的方法就行.这个其实就是多态.是重写.