简体   繁体   English

如何修复我的重写equals()方法

[英]How to fix my override equals() method

• I'm writing a override method for .equals() to compare two different classes of shapes. •我正在为.equals()写一个重写方法来比较两个不同类别的形状。

• The equals() will compare the dimensions of those shapes (EX: 4 X 4 X 4, equal to 4 X 4 X 4) •equals()将比较这些形状的尺寸(例如:4 X 4 X 4,等于4 X 4 X 4)

• Having trouble, already looked at other examples, and they don't apply to my program. •遇到麻烦了,已经看过其他示例,但这些示例不适用于我的程序。

• Please help with the return statement and if I set up other parts of it, that would also help :) •请帮助return语句,如果我设置了它的其他部分,那也会有帮助:)

By the way, I don't have the ability to use Eclipse. 顺便说一句,我没有使用Eclipse的能力。

Here's some code: 这是一些代码:

Method - My return statement is not done, I was trying to figure that out still. 方法-我的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();
 }

Rectangle class - 矩形类-

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 + ")";
}

} 

Part of main - 主要部分-

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

}

You've chosen wrong structure for inheritance of classes. 您为类的继承选择了错误的结构。 The parent class should be Box3 . 父类应该是Box3 Rectangle3 is a Box3 with height = 0; Rectangle3是高度为0的Box3 Cube3 is Box3 with equal sides: Cube3Box3等边:

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

And then you can add the following code to Box3 class for comparing Box3 instances: 然后,您可以将以下代码添加到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();
}

You should also override hashCode method, something like this: 您还应该重写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;
}

Your existing equals method is slightly confusing, I will explain whats wrong with it in the last section, but theres a lot in there that is unclear. 您现有的equals方法有点令人困惑,在上一节中,我将解释它的问题所在,但是其中有很多尚不清楚。

Writing an equals method. 编写一个equals方法。

An equals method is something which outputs true if two objects are equal by whatever definition you choose , so you have said that they need to both be rectangles and have the same class (Rectangle) and have equal dimentions. equals方法是一种东西,如果两个对象根据您选择的定义相等就输出true,因此您已经说过,它们必须都是矩形,并且具有相同的类(Rectangle),并且尺寸相等。 Here would be an equals method 这将是一个等于方法

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;

    }
}

Having seen Rectangle3 you should now be able to create one for Box3 as well. 看过Rectangle3之后​​,您现在还应该能够为Box3创建一个。

Overriding the hashcode 覆盖哈希码

Remember that when you override the equals method you need to override the hashcode also; 请记住,当您覆盖equals方法时,您还需要覆盖哈希码。 this is basically a quick check of equality. 这基本上是对相等性的快速检查。 A hashcode must return the same number for two equal objects and should probably equal non equal numbers for non equal objects. 哈希码必须为两个相等的对象返回相同的数字,并且对于不相等的对象可能应等于不相等的数字。 An example might be as follows: 示例如下:

@Override
public int hashCode() {
    int hash = 7;
    hash = 37 * hash + this.length;
    hash = 37 * hash + this.width;
    return hash;
}

A good IDE will be able to ble to generate a new HashCode for you. 一个好的IDE将能够为您生成一个新的HashCode。 Hashcodes are used by many collections (such as HashSet), failing to include it will cause you bugs down the road. 哈希码被许多集合(例如HashSet)使用,如果不包含哈希码,则会导致您在开发过程中产生错误。

Your equals method 你的平等方法

   @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();
 }

This is the equals method for Rectangle3 , you'll need to create annother equals method for Box3 , assuming Rectangle3's equals isn't suffificient (which it won't be as it doesn't include the 3rd dimention) 这是Rectangle3 equals方法,你需要创建annother等于为BOX3方法,假设Rectangle3的equals没有suffificient(它不会是因为它不包括3号渔政船)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM