1 package com.szxs.pet; 2 3 /** 4 * 宠物类 5 */ 6 public abstract class Pet { 7 private int health; 8 9 public int getHealth() {10 return health;11 }12 13 public void setHealth(int health) {14 this.health = health;15 }16 17 public abstract void toHospital();18 }
1 package com.szxs.pet; 2 3 /** 4 * 狗狗类 5 */ 6 public class Dog extends Pet { 7 /** 8 * 狗狗去医院 9 */10 public void toHospital() {11 System.out.println("狗狗看病");12 }13 }
1 package com.szxs.pet; 2 3 /** 4 * 企鹅类 5 */ 6 public class Penguin extends Pet { 7 8 /** 9 * 企鹅去医院10 */11 public void toHospital() {12 System.out.println("企鹅看病");13 }14 }
1 package com.szxs.pet; 2 3 /** 4 * 主人类 5 */ 6 public class Master { 7 /** 8 * 主人带宠物去看病 9 * 10 * @author 11 *12 */13 public void taketoHospital(Pet pet) {14 pet.toHospital();15 }16 }
1 package com.szxs.pet; 2 3 public class Test { 4 5 public static void main(String[] args) { 6 Master m=new Master(); 7 8 m.taketoHospital(new Dog()); 9 m.taketoHospital(new Penguin());10 11 }12 }