package J20250717.demo05; /** * @author WanJl * @version 1.0 * @title Hero * @description * @create 2025/7/17 */ public abstract class Hero { private String name; private int hp; private int mp; private int attack; private int speed; private int defenses; { //默认进行初始化赋值 this.hp=100; this.mp=50; this.attack=15; this.speed=5; this.defenses=10; } public Hero() { } public Hero(String name, int hp, int mp, int attack, int speed, int defenses) { this.name = name; this.hp = hp; this.mp = mp; this.attack = attack; this.speed = speed; this.defenses = defenses; debut(); } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getHp() { return hp; } public void setHp(int hp) { this.hp = hp; } public int getMp() { return mp; } public void setMp(int mp) { this.mp = mp; } public int getAttack() { return attack; } public void setAttack(int attack) { this.attack = attack; } public int getSpeed() { return speed; } public void setSpeed(int speed) { this.speed = speed; } public int getDefenses() { return defenses; } public void setDefenses(int defenses) { this.defenses = defenses; } /** * 普通攻击-平A * @param h */ public void a(Hero h){ //你攻击了另一位英雄, // (你的攻击力-对方的防御力)=对方受到的伤害 //对象的血量-受到的伤害的结果再赋值给对方的血量。 int sh=this.attack-h.getDefenses(); h.setHp(h.getHp()-sh); System.out.println(this.getName()+"对"+h.getName()+"进行了攻击,造成"+sh+"点伤害"); } @Override public String toString() { return "Hero{" + "name='" + name + '\'' + ", hp=" + hp + ", mp=" + mp + ", attack=" + attack + ", speed=" + speed + ", defenses=" + defenses + '}'; } /** * 英雄登场的方法 */ public abstract void debut(); }