2015-09-02 31 views
-1

我想操縱Java中的對象。爲了更好地解釋,我有一個叫做Creature的超類和兩個叫做Dragon and Warrior的子類。我爲每個龍和戰士創造了兩個對象來互相對抗。我該如何設置一個攻擊方法,該方法對一個對象造成傷害,並使用該數字將其從第二個對象的健康狀況中減去。操作OOP中的Java對象

請注意,戰士和龍都是亞類。

public class Creature { 

    private int health = 50; 
    private int attack; 
    private int level = 1; 
    private String name; 
    private Creature random; 
    private Creature random2; 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public int getHealth() { 
     if(health >= 3000) 
     { 
      health = 3000; 
     } 
     if(health <= 0) 
     { 
      health = 0; 
     } 
     return health; 
    } 

    public void setHealth(int health) { 
     this.health = health < 0 ? 0 : health; 
    } 

    public int getAttack() { 
     Random generator = new Random(); 
     attack = generator.nextInt(10) * level + 1; 
     return attack; 
    } 

    public void setAttack(int attack) { 
     this.attack = attack; 
    } 

    public int getLevel() { 
     if(level >= 60) 
     { 
      level = 60; 
     } 
     return level; 
    } 

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

    public boolean isAlive() { 
     if(getHealth() <= 0) 
     { 
      return false; 
     } 
     return true; 
    } 







    public String getWelcome() 
    { 
     String welcome = "Hello and welcome to Dragonslayer!"; 
     return welcome; 
    } 

    public String getStab() 
    { 
     String stab = "You choose to stab the dragon and dealt " + getAttack() + " damage. The dragon now has " + getHealth() + " health remaining."; 
     return stab; 
    } 

    public String getSlash() 
    { 
     String slash = "You choose to slash the dragon and dealt " + getAttack() + " damage. The dragon now has " + getHealth() + " health remaining."; 
     return slash; 
    } 

    public String getCut() 
    { 
     String cut = "You choose to cut the dragon and dealt " + getAttack() + " damage. The dragon now has " + getHealth() + " health remaining."; 
     return cut; 
    } 



    public static void main(String[] args) 
    { 
     Scanner in = new Scanner(System.in); 
     System.out.print("You come across the dragon and you have two options. Do you run or fight? "); 


     Creature kirsta = new Dragon(); 
     kirsta.setHealth(50); 

     Creature brenton = new Warrior(); 
     brenton.setHealth(50); 

     while(in.hasNextLine()) 
     { 
      switch(in.nextLine()) 
      { 
       case "run": 
        System.out.println("I am so sorry, you could not outrun the dragon, you have been killed!"); 
        break; 
       case "fight": 
        while(kirsta.isAlive() && brenton.isAlive()) 
        { 
         System.out.println("Do you want to stab, slash, or cut the dragon? "); 
         switch(in.nextLine()) 
         { 
          case "stab": 
           System.out.println("You choose to stab the dragon and dealt " + brenton.getAttack() + " damage. The dragon now has " + kirsta.getHealth() + " health remaining."); 
           break; 
          case "slash": 
           System.out.println("You choose to slash the dragon and dealt " + brenton.getAttack() + " damage. The dragon now has " + kirsta.getHealth() + " health remaining."); 
           break; 
          case "cut": 
           System.out.println("You choose to cut the dragon and dealt " + brenton.getAttack() + " damage. The dragon now has " + kirsta.getHealth() + " health remaining."); 
           break; 
          default: 
           System.out.println("I am sorry that is not valid, try again. "); 
         } 
        } 
        break; 
       default: 
        System.out.println("I am sorry that is not a valid choice. "); 
        break; 
      }//end of switch 
      if(brenton.isAlive() == false && kirsta.isAlive() == false) 
      { 
       System.out.println("It is a horrid day today, as both you and the dragon have fallen."); 
      } 
      else if(brenton.isAlive() == true && kirsta.isAlive() == false) 
      { 
       System.out.println("Congratulations you have killed the dragon, and survived!"); 
      } 
      else if(brenton.isAlive() == false && kirsta.isAlive() == true) 
      { 
       System.out.println("You have sadly fallen to the dragon, better luck next time."); 
      } 
      else 
      { 
       System.out.println("SOMETHING WENT WRONG!!!"); 
      } 
      break; 
     }//end of while loop in.hasNextLine(). 
    }//end of main 
}//end of class 
+2

這個問題太開放了。請顯示一些代碼,以獲得更快,更有幫助的回覆。 – andrewdleach

+4

目前爲止你有什麼代碼?什麼部分給你帶來困難? – khelwood

+2

嗯......這一切[看起來有點熟悉](http://stackoverflow.com/a/32342794/522444).... –

回答

3

假設你attack方法形成類似於這樣

public void attack(Creature otherCreature){ 
    otherCreature.decreaseHitPointsBy(this.attackValue); 
} 

使用看起來像

warrior.attack(dragon); 
+0

我會給這個嘗試,我不知道你可以使用類作爲參數值。 – TheDetailer

+0

這實際上很實用。通過這樣做,勇士們可以攻擊任何生物,而不僅僅是單一的硬編碼龍。:) –

+0

@ Brentonr25:他根本沒有上課。他正在定義參數的類型。請閱讀如何使用方法以及如何傳遞參數,因爲這是最基本的Java。 1+的答案順便說一句。 –

1

例如,您可以創建兩個類新擴展的生物,然後創建的實例他們使用new運營商。他們可以通過將參考傳遞給其他對象作爲攻擊方法的參數來進行相互作用:

class Creature { 
    protected int health = 100; 
    protected int strength = 10; 

    public void attack(Creature other) { 
     other.takeDamage(strength); 
    } 

    public void takeDamage(int amount) { 
     health -= amount; 
    } 
} 

class Warrior extends Creature { 
    protected int strength = 2; 

    //method specific to warrior 
    public void eatPotion() { 
     health = 100; 
    } 
} 

class Dragon extends Creature { 
    //You can simply override values in subclasses 
    protected int strength = 20; 
    protected int health = 1000; 
} 

Dragon dragon = new Dragon(); 
Warrior warrior = new Warrior(); 

warrior.attack(dragon); 
dragon.attack(warrior); 
warrior.eatPotion(); 
warrior.attack(dragon); 
+0

Nah,Marcin比我快,但也許我的例子會更詳細和有幫助:) –