如何用不同的方案创建更多的怪物

时间:2015-01-18 16:50:50

标签: java extends

你好我正在做我的作业创建一个基于文本的RPG。 我创建了一个包含Monster属性的Monster.class。 我的任务是创建具有不同属性的多个怪物(不同的Atk模式)。问题是我应该使用不同的类来扩展Monster类。我不知道如何使用Monster.class创建它们。

public class Monster extends Characters {

    public Monster() {
        this(160, 45, 0.6);
    }

    public Monster(int Hp, int atk, double hitChance){
        this.Hp = Hp;
        this.atk = atk;
        this.hitChance = hitChance;
    }

    public int attack(Player p) {
        if (Math.random() <= hitChance) {
            int damage = (int) (atk * (Math.random() + 1.0));
            p.takeDamage(damage);
            return damage;
        } else {
            return -1;
        }
    }

    public String toString(){
        return String.format("Gegner -- HP %d -- ATK %d%n",Hp, atk);
    }
}

2 个答案:

答案 0 :(得分:2)

您可以扩展Monster类并覆盖攻击方法。

public class Zombie extends Monster{

 public Zombie(int Hp, int atk, double hitChance){
    super(hp,atk,hitChance);
 }
 @Override
 public int attack(Player p) {
   // new awesome pattern
 }
}

答案 1 :(得分:1)

怎么样

public class NewMonster extends Monster{

  public NewMonster(int hp, int atk, double hitChance){
     super(hp, atk, hitChance);
  }
}