【问题标题】:How to make a player class that holds lives.(inheritance)如何制作一个拥有生命的玩家类。(继承)
【发布时间】:2012-03-24 06:21:57
【问题描述】:

我正在尝试创建一个有 10 条生命的玩家类,并且有一个damage 方法和一个 checkDeath 方法。damage 方法会从玩家那里获得生命,并且 check death 方法应该通过查看玩家是否死亡来检查玩家是否死亡如果玩家死亡或为假,则玩家的生命还不到一个返回 true。

这是我目前的代码:

public class Player {
    protected boolean death = true;
    Player mylife= new Player();//Dont know how to assign it 10 lives

public void damage(){
    if (myLife < 1) {
               return true;
            else { 
            return false;

}
public void checkDeath() {

}

}

【问题讨论】:

    标签: java


    【解决方案1】:
    public class Player {
        int liveCount = 10;
    
        public boolean damage() {
            if (myLife < 1)
                return true;
            else
                return false;
        }
    
        public boolean isDead() {
             return liveCount < 1;
        }
    }
    

    【讨论】:

      【解决方案2】:
      public class Player {
        int liveCount = 10;
      
        public boolean damage() {
          --liveCount;
          return isDead();
        }
      
        public boolean isDead() {
           return liveCount < 1;
        }
      
        public void boostLives(int moreLives) {
          liveCount += moreLives;
        }
      }
      

      【讨论】:

        【解决方案3】:
        public class Player{
            int lives=10;
            public boolean damage(){
              lives--;
              return isDead();
            }
            public boolean isDead(){
              return lives<1;
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2018-06-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-04-14
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多