•我正在为.equals()编写一个覆盖方法来比较两个不同类型的形状。
•equals()将比较这些形状的尺寸(EX:4 X 4 X 4,等于4 X 4 X 4)
•遇到麻烦,已经查看了其他示例,但它们并不适用于我的程序。
•请帮助返回声明,如果我设置了其他部分,那也会有所帮助:)
顺便说一句,我没有能力使用Eclipse。
以下是一些代码:
方法 - 我的返回语句没有完成,我试图弄明白。
@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 + ")";
}
Box class -
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);
}
答案 0 :(得分:0)
你为类的继承选择了错误的结构。父类应为Box3
。 Rectangle3
是Box3
,身高= 0; 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;
}
答案 1 :(得分:0)
你现有的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。 Hashcodes被许多集合(例如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 的等于方法,你需要为 Box3 创建另一个等于方法,假设Rectangle3的等于不够(它不会因为它不包括第三维)