【问题标题】:Exposure classes暴露等级
【发布时间】:2015-11-01 16:20:32
【问题描述】:

所以,当一个类与另一个类相互作用时,我尝试编写代码。这是“Tim”的职业战士和“Max”的职业巫师。控制台应该说“现在最大马力.. 50”或“现在蒂姆的马力.. 50 马力”,但控制台说“现在最大马力.. 25”或“现在蒂姆的马力.. 25 马力”.. 75 - 25 = 50; 100-50=50;但是说 75 - 25 = 25; 100-50=25; 请帮忙:c

import java.util.Random;

public class MAXvsTIMvoidVersion {

public static void main(String[] args) {

    warior Tim = new warior();
    Tim.agility = 100;
    Tim.attack = 25;
    Tim.hp = 100;
    Tim.Wariorlvl2 = true;

    witcher Max = new witcher();
    Max.mana = 150;
    Max.attack = 50;
    Max.hp = 75;
    Max.Witcherlvl1 = true;

    Random a = new Random();
    int b = a.nextInt(2);

            if (b == 0)
            {
                Tim.meleeAttack();
                System.out.print("Now Max's hp.." + Max.hp);
            }

            else if (b == 1)
            {
                Max.magicAttack();
                System.out.print("Now Tim's hp.." + Tim.hp);
            }
    }

}


class people
{
    static int hp;
    static int attack;
}

class witcher extends people
{
    int mana;
    boolean Witcherlvl1;
    boolean Witcherlvl2;
    void magicAttack()
    {
        warior.hp = warior.hp - witcher.attack;
        mana = mana - 20;
    }
}

class warior extends people
{
    int agility;
    boolean Wariorlvl1;
    boolean Wariorlvl2;
    void meleeAttack()
    {
        witcher.hp = witcher.hp - warior.attack;
        agility = agility - 20;
    }
}

【问题讨论】:

  • 注意naming conventions是:类名以大写字母开头,变量名以小写字母开头。如果您使用这些约定,则代码对于普通程序员来说变得更容易阅读,并且该站点上的代码标记也可以更好地工作......

标签: java class if-statement


【解决方案1】:
class People
{
    static int hp = 0;
    static int attack = 0;
}

看上面的代码。它告诉 hp 和 attack 是 people 类的一部分并且是静态的。所以,战士和巫师从人那里得到了相同的副本[阅读共享/相同]。所以,下面这段代码

warior Tim = new warior();
Tim.agility = 100;
Tim.attack = 25;
Tim.hp = 100;
Tim.Wariorlvl2 = true;

witcher Max = new witcher();
Max.mana = 150;
Max.attack = 50;
Max.hp = 75;
Max.Witcherlvl1 = true;

通过将攻击设置为 50 来提供覆盖效果。因此,在 meleeAttack()magicAttack() 中,您最终会减去 50 而不是适当的值。

要了解更多信息,请尝试设置 Max.attack = 0;

此外,您应该在meleeAttack()magicAttack() 中传递受害者的参考信息,这样会更清楚。像

void magicAttack( Warrior warrior );

我建议您从 People 类中删除静态修饰符。这应该可以解决问题,Warrior 和 Witcher 最终会得到他们自己的变量副本。

【讨论】:

  • 使用 OOP 来解决这个问题不是更好吗?
  • @AbtPst 我不太了解您要传达的内容。问题的核心是移除静态修饰符并让继承顺利进行,这本身就是 OOP 特性。
  • 是的,我只是认为另一种方法可以更好地构建类。
【解决方案2】:

这个结构怎么样

具有战士和巫师共同特征的人物类

class People
{
    int attack;
    int hp;
}

战士类

class Warrior extends People
{

  int agility;
  boolean Warriorlvl2;

  Warrior(int at, int hitp)
  {
    attack = at;
    hp = hitp;
  }

  void meleeAttack(People opponent)
  {
    opponent.hp = opponent.hp - this.attack;
  }
}

巫师类

class Witcher extends People
{
  int mana;
  boolean Witcherlvl1;

  Witcher(int at, int hitp)
 {
    attack = at;
    hp = hitp;
  }

  void magicAttack(People opponent)
  {
    opponent.hp = opponent.hp - this.attack;
  }
}

那么你可以有一个主类

public class MAXvsTIMvoidVersion {

public static void main(String[] args) {

    Warrior Tim = new Warrior(25,100);
    Tim.agility = 100;
    Tim.Wariorlvl2 = true;

    Witcher Max = new Witcher(50,75);
    Max.mana = 150;
    Max.Witcherlvl1 = true;

    Random a = new Random();
    int b = a.nextInt(2);

            if (b == 0)
            {
                Tim.meleeAttack(Max);
                System.out.print("Now Max's hp.." + Max.hp);
            }

            else if (b == 1)
            {
                Max.magicAttack(Tim);
                System.out.print("Now Tim's hp.." + Tim.hp);
            }
    }

}

通过这种方式,使用面向对象的方法,很容易实现你想要的行为。通读代码并尝试理解。如果你理解有困难,我可以指导你。

【讨论】:

  • 我认为 Panan 的回答比你的回答简单。但我试过你的鳕鱼,这也有效
  • 酷,我只是想展示面向对象的方法。快乐编码:)
猜你喜欢
  • 1970-01-01
  • 2011-11-06
  • 2018-06-19
  • 2012-08-08
  • 2023-01-26
  • 2012-04-26
  • 2011-03-26
  • 2018-08-11
  • 2020-06-22
相关资源
最近更新 更多