2012-07-02 138 views
1

我學習Java的工作,我們應該做運動規定如下:覆蓋Object類的equals和toString方法的Java die程序?

創建代表模具的類。創建滾動模的方法(從1到6的隨機數)

還重寫Object類提供的equals和toString方法。

從C++直接來的沒有Java經驗,我認爲第一部分是相對簡單的。但是,我不知道如何重寫equals和toString方法?

這裏是我的代碼,到目前爲止,任何建議將不勝感激:

package this; 

import java.lang.Object; 
public class Die 
{ 
    public static void main(String[] args) 
    { 
    int die; 
    die = (int)(Math.random()*6 + 1); 
    System.out.println (die); 
    } 
} 
+2

這是不是代表一個芯片的類。這是一個試圖模擬死亡的類。 –

+0

random(5)+ 1是做隨機函數的正確方法 –

回答

6

A模實例應該代表模具。 Die類不應該是啓動死亡的程序性應用程序。

Die具有一個狀態,它是當前的面值(1到6)。滾動它應該使其從當前面值變爲另一個面值。

toString()方法可以說它是一個死亡,並說它的當前面值。我不認爲重寫equals()是重點,因爲我不明白爲什麼死亡應該與其他死亡相等。但是如果它們具有相同的面值,則可以選擇使兩個模具相等。

1
public class Die { 
    int value; 
    public Die() { 
    roll; 
    } 

    public void roll() { 
    value = (int)(Math.random()*5 + 1) 
    } 

    @Override 
    public String toString() { 
    return "The current value is: " + value; 
    } 
} 
1

重寫equals()告訴什麼是真正提出了兩個對象相等。如果您不覆蓋equals(),則默認等於使用==。 覆蓋toString()爲程序員提供了定義打印對象時打印的內容的機會。如果toString()未被覆蓋,則使用默認值,該字符串是由對象爲實例的類的名稱,符號字符@和對象的哈希碼的無符號十六進制表示形成的字符串。

讓說我有一個對象模具

public class Die 
{ 
    private Long id; 

    private String face; 

/** 
* @return the id 
*/ 
public Long getId() { 
    return id; 
} 

/** 
* @param id the id to set 
*/ 
public void setId(Long id) { 
    this.id = id; 
} 
/** 
* @return the face 
*/ 
public String getFace() 
{ 
    return face; 
} 

/** 
* @param face the face to set 
*/ 
public void setFace(String face) 
{ 
    this.face = face; 
} 
//Overriding toString 
    /** 
    * @see java.lang.Object#toString() 
    */ 
    @Override 
    public String toString() 
    { 
     return "The value of the Die Face is = " + getFace(); 
    } 
    //Overriding equals 
    @Override 
    public boolean equals(final Object obj) 
    { 
     if (obj instanceof Die) 
     { 
      Die val = (Die) obj; 
      return (val.getFace().equals(this.face)); 
     } 
     return false; 
    } 

} 

讓我知道,如果你有問題。

+0

die.setFace(「OOP is about encapsulation」); –

+0

我開始更好地理解這一點。 Java和C++是相似的,但它實際上是我第一次使用Java,所以我覺得它超出了我的範圍。所以,在此之後,我會添加「公共靜態無效的主要(字符串參數[])」,並調用一個方法來擲骰子? – TimeBomb006

+0

是的。主要方法是創建Die('Die theDie = new Die()')的實例,然後多次擲骰子。不使用上述代碼作爲一個很好的例子:它doean't封裝的管芯的滾動,並具有不用於任何目的的ID。 –

1

這是一個不可改變的模具。

public class Die { 

    private int face; 

    private Die(int face) { 
     this.face = face; 
    } 
    public static Die roll() { 
     return new Die(Math.random(5) + 1); 
    } 

    private int getFace() { 
     return face; 
    } 

    public String toString() { 
     return "Die:" + face; 
    } 

    public boolean equals(Object obj) { 
     if (obj instanceof Die) { 
      return face == ((Die) obj).getFace(); 
     } else { 
      return false; 
     } 
    } 

    public int hashCode() { 
     return die; 
    } 
} 
0
package this; 

import java.lang.Object; 
public class Die 
{ 
    public static void main(String[] args) 
    { 
    int die; 
    die = (int)(Math.random()*6 + 1); 
    System.out.println (die); 
    } 
} 

應該看起來更像是這樣的:

package com.example; 

//don't need to import anything in the java.lang package you get this for free 

public class Die 
{ 
     private int value; //defaults to 0 

    public static void main(String[] args) 
    { 
     Die die = new Die(); 
     die.roll(); 
     System.out.println(die.toString()); 
    } 

    public String toString() 
    { 
     return "Value: " + value; 
    } 
    public int getValue() 
    { 
     return this.value; 
    } 
    public void roll() 
    { 
     this.value=(int)(Math.random(5)+1); 
    } 

public boolean equals(Object obj) { 
     if (obj instanceof Die) { 
      return value== ((Die) obj).getValue(); 
     } else { 
      return false; 
     } 
    } 

    public int hashCode() { 
     return die; 
    } 


}