2016-07-26 183 views
0

多麼驚人的平臺!我希望我不要問一個太愚蠢的問題,但我已經找到了一個沒有成功的答案。比較構造函數對象Java

問: 是否可以比較由構造函數創建的對象值?就像如果我想讓動物戰鬥並且比較對抗海誓山盟的「str」值。 我的目標是在Animal類中創建「戰鬥」方法,而不是主要方法。這樣一來,我可以把它像「dog.fight();

看我的代碼示例(抱歉,我的英語)

public class Animal { 

private int str; 
private int agi; 
private String name; 
private String eyeColour; 


public void set (int strenght, int agility, String _name){ 
    str = strenght; 
    agi = agility; 
    name = _name; 

} 
public String get(){ 
    System.out.println("Created a new animal named " + name +"! "); 
    System.out.println(name + "'s agility is " + agi); 
    System.out.println(name + "'s strenght is " + str); 

    return name + str + agi; 
}  

} 

import java.util.Scanner; 

public class Hello { 

public static void main(String[] args) { 

    Animal dog = new Animal(); 
    dog.set(8, 4, "Rambo"); 
    dog.get(); 

    System.out.println(""); 
    Animal cat = new Animal(); 
    cat.set(2, 9, "Felix"); 
    cat.get(); 


} 
} 
+0

是。使用'cat.str'和'dog.str'來訪問它們。 – bradimus

+0

並使用'str.equals'而不是'=='進行比較,因爲'=='比較了引用。 –

+0

我的目標是在Animal類中創建「戰鬥」方法,而不是主要方法。這樣,我可以稱它爲「dog.fight();」 這就是爲什麼我需要某種「obj1.str%obj2.str」我想 編輯:對不起,obj1.str.equals obj2.str // :) – Softy

回答

1

你需要創建一個方法,‘打’動物類的需要作爲參數的動物對象,並用它來恢復你的贏家的結果

這裏是代碼:

public class Animal { 

    private int str; 
    private int agi; 
    private String name; 
    private String eyeColour; 


    public void set (int strenght, int agility, String _name){ 
     str = strenght; 
     agi = agility; 
     name = _name; 

    } 
    public String get(){ 
     System.out.println("Created a new animal named " + name +"! "); 
     System.out.println(name + "'s agility is " + agi); 
     System.out.println(name + "'s strenght is " + str); 

     return name + str + agi; 
    } 

    public String fight(Animal rival){ 
     //Provide Comparison Logic Here 
     if(this.str>rival.str)return this.name; 
     if(this.str < rival.str)return rival.name; 
     return "No One "; 
    } 
} 


public class Hello { 

    public static void main(String[] args) { 

     Animal dog = new Animal(); 
     dog.set(8, 4, "Rambo"); 
     dog.get(); 

     System.out.println(""); 
     Animal cat = new Animal(); 
     cat.set(2, 9, "Felix"); 
     cat.get(); 

     System.out.println(dog.fight(cat)+" is the winner! "); 

    } 
} 

這裏小方注:

這裏使用構造函數而不是set方法好得多,因爲通常創建setter和getter來設置或獲取單個變量。

此外,最好更改get方法的名稱並覆蓋toString方法。

下面是修改後的代碼:

public class Animal { 

    private int str; 
    private int agi; 
    private String name; 
    private String eyeColour; 


    public Animal (int str, int agi, String name){ 
     this.str = str; 
     this.agi = agi; 
     this.name = name; 
    } 

    @Override 
    public String toString(){ 
     String description = "Created a new animal named " + name +"!\n"; 
     description+=name + "'s agility is " + agi+"\n"; 
     description+=name + "'s strenght is " + str; 
     return description; 
    } 

    public String fight(Animal rival){ 
     //Provide Comparison Logic Here 
     if(this.str>rival.str)return this.name; 
     if(this.str < rival.str)return rival.name; 
     return "No One "; 
    } 
} 


public class Hello { 

    public static void main(String[] args) { 

     Animal dog = new Animal (8, 4, "Rambo"); 
     System.out.println(dog); 

     System.out.println(); 
     Animal cat = new Animal (2, 9, "Felix"); 
     System.out.println(cat); 

     System.out.println(dog.fight(cat)+" is the winner! "); 

    } 
} 
+0

我喜歡它,謝謝先生! – Softy

+0

最好不要有兩隻同名的動物:)或者叫做「Draw」的動物:)) – Matt

+0

不客氣!我已經更新了答案! –