Почему метод возвращает некорректное имя?
Есть абстрактный класс:
public abstract class BaseHero {
protected int maxHp;
protected static int hp;
protected static int damage;
protected static int armor;
protected static double satiety;
protected static double blockChance;
protected static String name;
protected static int attackDistance;
public BaseHero(int hp, int damage, int armor, double satiety, double blockChance, String name, int attackDistance) {
this.hp = this.maxHp = hp;
this.damage = damage;
this.armor = armor;
this.satiety = satiety;
this.blockChance = blockChance;
this.name = name;
this.attackDistance = attackDistance;
}
public String getInfo() {
return String.format("Name: %s Hp: %d MaxHp: %d",
this.name, this.hp, this.maxHp);
}
public void GetDamage(int damage) {
if (this.hp - damage > 0) {
this.hp -= (1-this.blockChance) * damage * (1 - (this.satiety / 100));
}
}
public void Attack(BaseHero target) {
int damage = BaseHero.damage;
target.GetDamage(damage);
}
}
Так же два класса наследника.
public class Magician extends BaseHero {
private static int mana;
public Magician(String name) {
super(hp = 100,
damage = 10,
armor = 60,
satiety = 1,
blockChance = new Random().nextDouble(.01f, 1),
name = name,
attackDistance = 10);
this.mana = 100;
}
@Override
public String getInfo() {
return String.format("%s Mana: %d",super.getInfo(), this.mana);
}
}
public class Sniper extends BaseHero {
public Sniper(String name) {
super(hp = 100,
damage = 60,
armor = 60,
satiety = .6f,
blockChance = new Random().nextDouble(.01f, 1),
name = name,
attackDistance = 10);
}
@Override
public String getInfo() {
return super.getInfo();
}
}
Почему при выполнении метода Main:
public class Main {
public static void main(String[] args) {
BaseHero sniper = new Sniper("Sniper");
System.out.println(sniper.getInfo());
BaseHero magician = new Magician("Magical");
System.out.println(magician.getInfo());
magician.Attack(sniper);
System.out.println(sniper.getInfo());
sniper.Attack(magician);
System.out.println(magician.getInfo());
}
}
Выводится некорректное значение name
:
Name: Sniper Hp: 100 MaxHp: 100
Name: Magical Hp: 100 MaxHp: 100 Mana: 100
Name: Magical Hp: 97 MaxHp: 100
Name: Magical Hp: 94 MaxHp: 100 Mana: 100
Источник: Stack Overflow на русском