java - 如何用不同的方案创造更多的怪物

标签 java extends

您好,我目前正在做作业,创建一个基于文本的角色扮演游戏。 我创建了一个包含 Monster 属性的 Monster.class。 我的任务是创建多个具有不同属性(不同攻击模式)的怪物。问题是我应该使用扩展怪物类的不同类。我不知道如何使用 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);
    }
}

最佳答案

您可以扩展 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
 }
}

关于java - 如何用不同的方案创造更多的怪物,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28012255/

相关文章:

java - Java扩展ArrayList时如何添加map和filter?

angular - TypeScript 合并和导出命名空间错误

JAVA 比较何时计算为 boolean 值?在运行时还是在编译时?

javascript - ES6/ES7扩展功能并导出扩展

java - 使用泛型参数类型重载构造函数(扩展与扩展和接口(interface))

java - 在插件中和通过 Tomcat 覆盖 Spring Boot application.properties 属性?

c++ - 模板可以扩展 C++ 函数中的类吗?

java - "virtual"个文件

java - EJB 2.0 CMP : exception scenario in multiple remove calls

java - 如何为 : '2.0.0-alpha1' in Spring boot 版本启用登录 org.slf4j