簡體   English   中英

覆蓋Object類的equals和toString方法的Java die程序?

[英]Java die program that overrides equals and toString methods of Object class?

我正在上班學習Java,我們應該做的練習說明如下:

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

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

直接來自沒有Java經驗的C ++,我認為第一部分相對簡單。 但是,我不確定如何重寫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);
    }
}

模具實例應代表模具。 骰子類不應是啟動骰子的過程性應用程序。

模具的狀態是其當前面值(1到6)。 滾動它可以使它從當前面值變成另一個面值。

它的toString()方法可以說這是一個骰子,並可以說出它當前的面值。 我真的看不到覆蓋equals()的意義,因為我看不到為什么一個骰子應該等於另一個骰子。 但是,如果它們具有相同的面值,則可以選擇使兩個模具相等。

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;
  }
}

覆蓋equals()告訴我們實際上使兩個對象相等的原因。 如果不重寫equals() ,則使用使用==的默認equals。 覆蓋toString()使程序員有機會定義在打印對象時打印出的內容。 如果未重寫toString() ,則使用默認值,該默認值是一個字符串,該字符串由對象為實例的類的名稱,符號字符@和對象的哈希碼的無符號十六進制表示組成。

假設我有一個物體Die

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;
    }

}

如果您有任何問題,請告訴我。

這是一成不變的骰子。

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;
     }
}

有關覆蓋方法的Java教程將是一個不錯的起點。

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;
     }


}  

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM