多态:同一个引用类型,使用不同的实例而执行不同的操作
使用多态的两种形式:
1.使用父类作为方法形参实现多态
2.使用父类作为方法返回值实现多态
1.主人给宠物喂食功能
不同宠物吃的东西不同
主人可以喂养不同类型宠物
2.主人与宠物玩耍功能
主人和狗狗玩接飞盘游戏,狗狗健康值减少10,与主人亲密度增加5;主人和企鹅玩游泳游戏,企鹅健康值减少10,与主人亲密度增加5
宠物抽象类;Pet
package Animal; /** * 宠物类Pet * * @author Administrator * */ public abstract class Pet { protected String name; protected int health; protected int love; public Pet() { } public void print() { System.out.println("宠物的自白: 我的名字叫 " + this.name + ",健康值 " + this.health + "和主人的亲密度" + this.love + "。"); } public abstract void eat(); public Pet(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getHealth() { return health; } public void setHealth(int health) { this.health = health; } public int getLove() { return love; } public void setLove(int love) { this.love = love; } }
狗类Dog
package Animal; /** * 狗类Dog * @author Administrator * */ public class Dog extends Pet{ private String strain; public Dog(String name,String strain){ super(name);//构造 this.strain=strain; } public void print(){ super.print();//调用父类print方法 System.out.println("我是一只 "+this.strain+", "); } public void eat(){ super.health=super.health+3; System.out.println("狗狗"+super.name+"吃饱了!健康值增加3"); } //实现接飞盘功能 public void catchingFlydisc(){ System.out.println("狗狗"+super.name+"正在接飞盘。"); super.health+=10; super.love+=5; } public String getStrain() { return strain; } public void setStrain(String strain) { this.strain = strain; } }
企鹅类:Penguin
package Animal; /** * 企鹅Penguin类 * * @author Administrator * */ public class Penguin extends Pet { private String sex; public Penguin(String name, String sex) { super(name); this.sex = sex; } public void print() { super.print(); System.out.println("性别是" + this.sex + ","); } public void eat() { super.health += 5; System.out.println("企鹅" + super.name + "吃饱了,健康值增加5"); } // 实现游泳功能 public void swimming() { System.out.println("企鹅" + super.name + "正在游泳。"); super.health -= 10; super.love += 5; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } }
主人类:Master
package Animal; /** * Master主人类 * * @author Administrator * */ public class Master { private String name; private int money = 0; public Master(String name, int money) { this.name = name; this.money = money; } // 喂食功能 public void feed(Pet pet) { pet.eat(); } // 玩耍功能 public void play(Pet pet) {// if (pet instanceof Dog) { Dog dog = (Dog) pet;// 强制转换 dog.catchingFlydisc(); } else if (pet instanceof Penguin) { Penguin pen = (Penguin) pet; pen.swimming(); } } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getMoney() { return money; } public void setMoney(int money) { this.money = money; } }
测试Test类
package Animal; /** * 实现调试功能 * @author Administrator * */ public class Test { public static void main(String[] args) { Dog dog=new Dog("欧欧","雪纳瑞"); Penguin pen=new Penguin("楠楠","Q妹妹"); Master master =new Master("王先生",100); master.feed(dog); master.feed(pen); master.play(pen); } }