1 class Animala{ 2 void makeNoise(){ 3 System.out.println("动物在嘶吼"); 4 } 5 } 6 class Dogs extends Animala{ 7 void makeNoise(){ //重写 8 System.out.println("狗的叫声是汪汪汪"); 9 } 10 } 11 class Cat extends Animala{ 12 void makeNoise(){ 13 System.out.println("猫的叫声是喵呜呜呜呜"); 14 } 15 } 16 class Vet{ 17 public void giveShot(Animala a){ 18 a.makeNoise(); 19 } 20 } 21 public class Canshufanhui { 22 void start(){ 23 Vet v = new Vet(); 24 Dogs d = new Dogs(); 25 Cat c = new Cat(); 26 v.giveShot(d); //giveshot接受的是animala类型的参数,Dogs继承自animala,利用了参数多态 27 v.giveShot(c); 28 } 29 public static void main(String[] args){ 30 Canshufanhui p = new Canshufanhui(); 31 p.start(); 32 } 33 34 }