返回方法值Java

返回方法值Java,java,Java,我有一个名为Hero的类,在那里我创建了DrowRanger 我正在使用setter和getter,我想返回DrowRanger的一些统计数据 既然我实现了一个包含这些方法的接口,然后定义了这个类中的每个方法,那么就没有办法只返回方法getHealth(),而只返回DrowRanger的Health 比如DrowRanger.getHealth()或类似的东西 这是英雄类: public class Hero implements NPC { private int level

我有一个名为
Hero
的类,在那里我创建了
DrowRanger

我正在使用setter和getter,我想返回DrowRanger的一些统计数据

既然我实现了一个包含这些方法的接口,然后定义了这个类中的每个方法,那么就没有办法只返回方法
getHealth()
,而只返回
DrowRanger
的Health

比如
DrowRanger.getHealth()或类似的东西

这是英雄
类:

public class Hero implements NPC {

        private int level;
        private String primaryAttribute;
        private String attackType;
        private String ability1;
        private String ability2;
        private String ability3;
        private String ability4;
        private double strength;
        private double strengthMultiplier;
        private double agility;
        private double agilityMultiplier;
        private double intelligence;
        private double intelligenceMultiplier;
        private int health;
        private int mana;
        private int damageMin;
        private int damageMax;
        private int range;
        private double armor;
        private int movement;

        //default constructor
        public Hero(
                int level,
                String primaryAttribute,
                String attackType,
                String ability1,
                String ability2,
                String ability3,
                String ability4,
                double strength,
                double strengthMultiplier,
                double agility,
                double agilityMultiplier,
                double intelligence,
                double intelligenceMultiplier,
                int health,
                int mana,
                int damageMin,
                int damageMax,
                int range,
                double armor,
                int Movement
                     ){

        } // End Constructor

        public static void main (String[] args) {
            getHealth(DrowRanger);

            } // End Main Method

        private static Object DrowRanger() {
        Hero DrowRanger = new Hero(
              0,
              "Agility",
              "Ranged",
              "Frost Arrows",
              "Gust",
              "Precision Aura",
              "Marksmanship",
              17,
              1.9,
              26,
              1.9,
              15,
              1.4,
              473,
              195,
              44,
              55,
              625,
              0.64,
              300);

        return DrowRanger;
} // End DrowRanger Method


        // getters and setters - required to implement ALL from interface
        public int getLevel() {
            return this.level;
        }

        public String getPrimaryAttribute() {
            return this.primaryAttribute;
        }

        public String getAttackType() {
            return this.attackType;
        }

        public String getAbility1() {
            return this.ability1;
        }

        public String getAbility2() {
            return this.ability2;
        }

        public String getAbility3() {
            return this.ability3;
        }

        public String getAbility4() {
            return this.ability4;
        }

        public double getStrength() {
            return this.strength;
        }

        public double getStrengthMultiplier() {
            return this.strengthMultiplier;
        }

        public double getAgility() {
            return this.agility;
        }

        public double getAgilityMultiplier() {
            return this.agilityMultiplier;
        }

        public double getIntelligence() {
            return this.intelligence;
        }

        public double getIntelligenceMultiplier() {
            return this.intelligenceMultiplier;
        }

        public int getHealth() {
            return this.health;
        }

        public int getMana() {
            return this.mana;
        }

        public int getDamageMin() {
            return this.damageMin;
        }

        public int getDamageMax() {
            return this.damageMax;
        }

        public int getRange() {
            return this.range;
        }

        public double getArmor() {
            return this.armor;
        }

        public int getMovement() {
            return this.movement;
        }

    // This is where the setters are.

        public void setLevel(int level) {
            this.level = level;
        }

        public void setPrimaryAttribute(String primaryAttribute) {
            this.primaryAttribute = primaryAttribute;
        }

        public void setAttackType(String attackType) {
            this.attackType = attackType;
        }

        public void setAbility1(String ability1) {
            this.ability1 = ability1;
        }

        public void setAbility2(String ability2) {
            this.ability2 = ability2;
        }

        public void setAbility3String(String ability3) {
            this.ability3 = ability3;
        }

        public void setAbility4(String ability4) {
            this.ability4 = ability4;
        }

        public void setStrength(double strength) {
            this.strength = strength;
        }

        public void setStrengthMultiplier(double strengthMultiplier) {
            this.strengthMultiplier = strengthMultiplier;
        }

        public void setAgility(double agility) {
            this.agility = agility;
        }

        public void setAgilityMultiplier(double agilityMultiplier) {
            this.agilityMultiplier = agilityMultiplier;
        }

        public void setIntelligence(double intelligence) {
            this.intelligence = intelligence;
        }

        public void setIntelligenceMultiplier(double intelligenceMultiplier) {
            this.intelligenceMultiplier = intelligenceMultiplier;
        }

        public void setHealth(int health) {
            this.health = health;
        }

        public void setMana(int mana) {
            this.mana = mana;
        }

        public void setDamageMin(int damageMin) {
            this.damageMin = damageMin;
        }

        public void setDamageMax(int damageMax) {
            this.damageMax = damageMax;
        }

        public void setRange(int range) {
            this.range = range;
        }

        public void setArmor(double armor) {
            this.armor = armor;
        }

        public void setMovement(int movement) {
            this.movement = movement;
        }

    } // End Character Class
这行吗

public static void main(String[] args) {
    ((Hero) DrowRanger()).getHealth(); // This gets a drowRanger with the "DrowRanger()" method and calls "getHealth()" on it
}
此外,您应该尝试遵循Java命名约定,即:变量和方法以小写字母开头,对象和类名以大写字母开头


如果您不想强制转换对象,还可以使
DrowRanger()
返回一个
Hero
,而不是
Object
,以符合Java命名约定。不纠正这一点并再次发布是不理想的。我确实遵守了,这是一个完全不同的问题。请随意阅读,不要盲目反对。谢谢我确实读过。小心在
PascalCase
-
DrowRanger
中仍然有一个实例变量。你仍然有错误的缩进。请使用IDE并格式化您的代码。我猜您会想将DrowRanger()的签名更改为:
public static Hero DrowRanger()
…而且您的Hero构造函数实际上没有初始化任何变量…在eclipse中,它会给我和getHealth上的错误。它提供给DrowRanger增加演员阵容…@Boristespider你介意帮忙,而不是否决这个问题和唯一的答案,然后批评每个人。它是如何编译的?对不起,我一定遗漏了什么,我看到每个开头都有一个右括号,缺少的括号在哪里?基本上这应该是
((英雄)DrowRanger()).getHealth()