欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

JAVA基础-练习-多态

程序员文章站 2024-03-21 19:20:40
...

练习-多态

题目:
immortal是不朽的,不死的意思

mortal就是终有一死的,凡人的意思

  1. 设计一个接口
    接口叫做Mortal,其中有一个方法叫做die

  2. 实现接口
    分别让ADHero,APHero,ADAPHero这三个类,实现Mortal接口,不同的类实现die方法的时候,都打印出不一样的字符串

  3. 为Hero类,添加一个方法,在这个方法中调用 m的die方法。

    public void kill(Mortal m)

  4. 在主方法中
    首先实例化出一个Hero对象:盖伦
    然后实例化出3个对象,分别是ADHero,APHero,ADAPHero的实例
    然后让盖伦 kill 这3个对象

**练习-多态 答案

package charactor;

import property.Item;
import property.Weapon;
import property.LifePotion;
import property.MagicPotion;

public class Hero {	//英雄
	
	private int id;
	public String name;	//姓名
	protected float hp;	//血量
	float maxHP;
	float armor;	//护甲
	int moveSpeed;	//移动速度
	
	int killNum;	//杀人次数
	int assistNum;	//助攻次数
	int secondPitNum; //补刀数
	
	int money;	//金钱
	
	String killspech;	//杀人后说的话
	String deadspech;	//被杀后说的话
	
	static String copyright;
	
	//物品栏的容量
	public static int itemCapacity = 8;	
	
	static {
		itemCapacity = 6;
	}

	
	public Hero(String name) {
		this.name = name;
	}
	
	public Hero(String name,float hp) {
		this(name);
		this.hp = hp;
	}
	
	public void die() {
		hp = 0;
	}
	
	public static void battleWin() {
		System.out.println("battle win");
	}
		
//练习-构造方法
//		public Hero(String heroName,float heroHP,float heroArmor,int heroMoveSpeed) {
//			name = heroName;
//			hp = heroHP;
//			armor = heroArmor;
//			moveSpeed = heroMoveSpeed;
//		}
		
//练习-构造方法(this)
		public Hero(String name,float hp,float armor,int moveSpeed) {
			this(name,hp);
			this.armor = armor;
			this.moveSpeed = moveSpeed;
		}
		
		//获得护甲值
		float getArmor() {
			return armor;
		}
		
		//坑队友
		void keng() {
			System.out.println("坑队友!");
		}
		
		//增加移动速度
		void addSpeed(int speed) {
			//在原来的基础上增加移动速度
			moveSpeed = moveSpeed + speed;
		}
		
		//超神
		void legendary() {
			System.out.println("超神");
		}
		
		//获取当前的血量
		float getHp() {
			return hp;
		}
		
		//回血
		void recovery(float blood) {
			hp = hp + blood; 
		}
		
		//装备一把武器
		public void equip(Weapon w) {
			
		}
		
		public void useItem(Item i) {
			i.effect();
		}
//练习-多态
		public void kill(Mortal m) {
			m.die();
		}
		
	public static void main(String[] args) {
		Hero garen = new Hero("盖伦",616.28f,350,100);		
		ADHero bot1 = new ADHero("甲");
		APHero bot2 = new APHero("乙");
		ADAPHero bot3 = new ADAPHero("丙");
		
		garen.kill(bot1);
		garen.kill(bot2);
		garen.kill(bot3);	
		}

//练习-单例模式(饿汉式)
		private Hero() {
			
		}
		
		private static Hero instance = new Hero();
		
		public static Hero getInstance() {
			return instance;
		}
}

package charactor;

public class ADHero extends Hero implements AD,Mortal{	//物理攻击英雄
	public ADHero(String name) {
		super(name);
	}

	public void attack() {
		System.out.println(name + " 进行了一次攻击 ,但是不确定打中谁了");
	}

	// 可变数量的参数
	public void attack(Hero... heros) {
		for (int i = 0; i < heros.length; i++) {
			System.out.println(name + " 攻击了 " + heros[i].name);

		}
	}
	@Override
	public void physicAttack() {
		System.out.println("进行物理攻击");
	}

//练习-多态	
	@Override
	public void die() {
		System.out.println(name+":最强AD去去就来");
	}

	public static void main(String[] args) {
		ADHero bh = new ADHero("赏金猎人");
		
		Hero h1 = new Hero("盖伦");
		Hero h2 = new Hero("提莫");

		bh.attack(h1);
		bh.attack(h1, h2);

	}

}
package charactor;

public class APHero extends Hero implements AP,Mortal{
	public APHero(String name) {
		super(name);
	}

	@Override
	public void magicAttack() {
		System.out.println("进行魔法攻击");
	}
//练习-多态
	@Override
	public void die() {
		System.out.println(name+":伟大的AP死了");
	}
}

package charactor;

public class ADAPHero extends Hero implements AD,AP,Mortal {
	public ADAPHero(String name) {
		super(name);
	}

	@Override
	public void magicAttack() {
		System.out.println("进行魔法攻击");
	}
	
	@Override
	public void physicAttack() {
		System.out.println("进行物理攻击");
	}
//练习-多态	
	@Override
	public void die() {
		System.out.println(name+":真糟糕,我没了还咋玩");
	}
}
package charactor;

public interface Mortal {
	public void die();
}