java多态性练习题---主人和狗狗玩接飞盘游戏,狗狗健康值减少10,与主人亲密度增加5 主人和企鹅玩游泳游戏,企鹅健康值减少10,与主人亲密度增加5

/*

需求说明:
主人和狗狗玩接飞盘游戏,狗狗健康值减少10,与主人亲密度增加5
主人和企鹅玩游泳游戏,企鹅健康值减少10,与主人亲密度增加5
提示:
Dog类添加catchingFlyDisc()方法,实现接飞盘功能
Penguin类添加swimming()方法,实现游泳功能
主人添加play(Pet pet)方法
如果pet代表Dog就玩接飞盘游戏
如果pet代表Penguin就玩游泳游戏

*/

Pet类

public class Pet {
	private String name;
	protected int health=100;//健康值
	protected int love = 0;//亲密度
	
	public Pet() {
		super();
	}
	
	public Pet(String name, int health, int love) {
		super();
		this.name = name;
		this.health = health;
		this.love = love;

	}

//第二种写法:

/*

public abstract void play();

*/





	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类:

/**
 * 主人和狗狗玩接飞盘游戏,狗狗健康值减少10,与主人亲密度增加5
 * Dog类添加catchingFlyDisc()方法,实现接飞盘功能
 * @author Administrator
 *
 */

public class Dog extends Pet {
	private String strain;//品种


	public Dog() {
		super();
	}
	


	public Dog(String strain) {
		super();
		this.strain = strain;

	}

//第二种写法

/*

	@Override
	public void play() {
		// TODO Auto-generated method stub
		catchingFlyDisc(); //调用正在接飞盘的类
	}

*/

	
	public void catchingFlyDisc(){
		System.out.println("小狗正在接飞盘!");
		super.health =super.health-10; 
		System.out.println("健康值:" + super.health);
		super.love = super.love +5;
		System.out.println("亲密度:" + super.love);
	}
	
	public String getStrain() {
		return strain;
	}

	public void setStrain(String strain) {
		this.strain = strain;
	}
}

Penguin类:

/**
 * 主人和企鹅玩游泳游戏,企鹅健康值减少10,与主人亲密度增加5
 * Penguin类添加swimming()方法,实现游泳功能
 * @author Administrator
 *

 */

public class Penguin extends Pet {
	private String sex;


	public Penguin() {
		super();
	}
	public Penguin(String sex) {
		super();
		this.sex = sex;

	}

//第二种写法

/*

@Override
	public void play() {
		// TODO Auto-generated method stub
		swimming();//调用正在游泳的类
	}

*/

	public void swimming(){
		System.out.println("企鹅正在接飞盘!");
		super.health =super.health-10; 
		System.out.println("健康值:" + super.health);
		super.love = super.love +5;
		System.out.println("亲密度:" + super.love);
	}
	
	public String getSex() {
		return sex;
	}


	public void setSex(String sex) {
		this.sex = sex;
	}
}

Master类:

public class Master {
	private String name;
	private int age;
	public Master() {
		super();
	}
	public Master(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}
	public void play(Pet pet){
		 if(pet instanceof Dog){
	            Dog dog = (Dog)pet;
	            dog.catchingFlyDisc();
	        }else if(pet instanceof Penguin){
	            Penguin p = (Penguin)pet;
	            p.swimming();
	        }

	}

//第二种写法

/*

public void play(Pet pet) {
		pet.play();
	}

*/



	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
}

测试类Test:

public class Test {


	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Master m = new Master();
		
		m.play(new Dog());
		m.play(new Penguin());
	}
}

  • 8
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值