[英]How to fix my override equals() method
•我正在為.equals()寫一個重寫方法來比較兩個不同類別的形狀。
•equals()將比較這些形狀的尺寸(例如:4 X 4 X 4,等於4 X 4 X 4)
•遇到麻煩了,已經看過其他示例,但這些示例不適用於我的程序。
•請幫助return語句,如果我設置了它的其他部分,那也會有幫助:)
順便說一句,我沒有使用Eclipse的能力。
這是一些代碼:
方法-我的return語句還沒有完成,我正在嘗試找出答案。
@Override
public boolean equals( Object ob )
{
// instanceof is not in the AP Java subset
if ( ! (ob instanceof Rectangle3) )
{
return false;
}
Rectangle3 that = (Rectangle3)ob;
return Objects.equals()
return t.getReal() == getReal() && t.getImag() == getImag();
}
矩形類-
private int length;
private int width;
public Rectangle3(int l, int w)
{
length = l;
width = w;
}
public int getLength()
{
return length;
}
public int getWidth()
{
return width;
}
public String toString()
{
return "(" + length + " X " + width + ")";
}
箱類-
public class Box3 extends Rectangle3
{
// instance variables
private int height;
/**
* Constructor for objects of class box
*/
public Box3(int l, int w, int h)
{
// call superclass
super(l, w);
// initialise instance variables
height = h;
}
// return the height
public int getHeight()
{
return height;
}
public String toString()
{
return "(" + getLength() + " X " + getWidth() + " X " + height + ")";
}
}
主要部分-
public static void main(String []args)
{
Rectangle3 one = new Rectangle3(5, 20);
Box3 two = new Box3(5, 5, 5);
Cube3 three = new Cube3(5, 5, 5);
// print
System.out.println(" Dimensions: ");
showEffectBoth(one);
showEffectBoth(two);
showEffectBoth(three);
}
public static void showEffectBoth(Rectangle3 r)
{
System.out.println(r.getClass().getName() + " - " + r);
}
您為類的繼承選擇了錯誤的結構。 父類應該是Box3
。 Rectangle3
是高度為0的Box3
; Cube3
是Box3
等邊:
class Box3{
int length;
int height;
int width;
Box3()
{
}
Box3(int length, int height, int width)
{
this.length = length;
this.height = height;
this.width = width;
}
}
class Rectangle3 extends Box3{
Rectangle3(int length, int width)
{
super(length, 0, width);
}
}
class Cube3 extends Box3{
Cube3(int side)
{
super(side, side, side);
}
}
然后,您可以將以下代碼添加到Box3
類中,以比較Box3
實例:
@Override
public boolean equals(Object ob)
{
if (ob == null) return false;
if (ob == this) return true;
if (!(ob instanceof Box3))return false;
Box3 that = (Box3) ob;
return that.getHeight() == this.getHeight()
&& that.getWidth() == this.getWidth()
&& that.getLength() == this.getLength();
}
您還應該重寫hashCode
方法,如下所示:
@Override
public int hashCode() {
int hash = 1;
hash = hash * 17 + this.length;
hash = hash * 31 + this.width;
hash = hash * 13 + this.height;
return hash;
}
您現有的equals方法有點令人困惑,在上一節中,我將解釋它的問題所在,但是其中有很多尚不清楚。
equals方法是一種東西,如果兩個對象根據您選擇的定義相等就輸出true,因此您已經說過,它們必須都是矩形,並且具有相同的類(Rectangle),並且尺寸相等。 這將是一個等於方法
public class Rectangle3{
private int length;
private int width;
public Rectangle3(int l, int w)
{
length = l;
width = w;
}
public int getLength()
{
return length;
}
public int getWidth()
{
return width;
}
@Override
public boolean equals(Object other){
if (!(other instanceof Rectangle3)){
return false;
}
Rectangle3 otherRectangle=(Rectangle3)other;
return this.length==otherRectangle.length && this.width==otherRectangle.width;
}
}
看過Rectangle3之后,您現在還應該能夠為Box3創建一個。
請記住,當您覆蓋equals方法時,您還需要覆蓋哈希碼。 這基本上是對相等性的快速檢查。 哈希碼必須為兩個相等的對象返回相同的數字,並且對於不相等的對象可能應等於不相等的數字。 示例如下:
@Override
public int hashCode() {
int hash = 7;
hash = 37 * hash + this.length;
hash = 37 * hash + this.width;
return hash;
}
一個好的IDE將能夠為您生成一個新的HashCode。 哈希碼被許多集合(例如HashSet)使用,如果不包含哈希碼,則會導致您在開發過程中產生錯誤。
@Override
public boolean equals( Object ob )
{
// this is good, first check if its the right type
if ( ! (ob instanceof Rectangle3) )
{
return false;
}
//poor naming convention but again the right idea, we need to cast to Rectangle3 to use its methods
Rectangle3 that = (Rectangle3)ob;
return Objects.equals()
//this just shouldn't be here, you've also missed a ;
return Objects.equals()
//t is not defined, possible you mean that, but in any case
//getReal() and getImag() are not defined in the rectangle class
return t.getReal() == getReal() && t.getImag() == getImag();
}
這是Rectangle3 equals方法,你需要創建annother等於為BOX3方法,假設Rectangle3的equals沒有suffificient(它不會是因為它不包括3號漁政船)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.