Почему метод возвращает некорректное имя?

Рейтинг: -1Ответов: 1Опубликовано: 29.06.2023

Есть абстрактный класс:

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

Ответы

▲ 0Принят

Нужно убрать static, а именованных аргументов как в Python в Java нет.

public abstract class BaseHero {
    protected int maxHp;
    protected int hp;
    protected int damage;
    protected int armor;
    protected double satiety;
    protected double blockChance;
    protected String name;
    protected 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 = this.damage;
        target.getDamage(damage);
    }
}
import java.util.Random;

public class Magician extends BaseHero {
    private int mana;
    public Magician(String name) {
        super(100, 10, 60, 1, new Random().nextDouble() * 9f + .01f, name, 10);
        this.mana = 100;
    }
    @Override
    public String getInfo() {
        return String.format("%s Mana: %d", super.getInfo(), this.mana);
    }
}
import java.util.Random;

public class Sniper extends BaseHero {
    public Sniper(String name) {
        super(100, 60, 60, .6f, new Random().nextDouble() * 9f + .01f, name, 10);
    }
}