简体   繁体   English

覆盖equals()方法以检查共享继承的对象中的维相等性

[英]Overriding the equals() method to check for dimensional equality in objects sharing inheritance

I've provided all four classes involved in this particular issue for those who might desire them. 我已为可能需要它们的人提供了涉及此特定问题的所有四个班级。

The primary issue is found within the main() method, as the last line in the main method throws an error which reads as follows: 主要问题可在main()方法中找到,因为main方法的最后一行会引发错误,内容如下:

method equals in class java.lang.Object cannot be applied to given types; 类java.lang.Object中的equals方法不能应用于给定类型;

required: java.lang.Object 必需:java.lang.Object

found: Box2, Cube2 找到:Box2,Cube2

reason: actual and formal argument lists differ in length. 原因:实际参数和正式参数列表的长度不同。

The operator that you use here cannot be used for the type of value that you are using it for. 您在此处使用的运算符不能用于您使用它的值的类型。 You are either using the wrong type here, or the the wrong operator. 您在此处使用错误的类型或错误的运算符。

I believe the issue may stem from my approach in overriding the equals() method (which is located within the Box class) being incorrect. 我认为问题可能源于我的方法不正确,即重写equals()方法(位于Box类中)。

This is the method which is intended to print the desired output, but is instead returning an error. 此方法旨在打印所需的输出,但返回错误。

Any insight on how I may rewrite this method properly would be highly appreciated, as I am at a loss. 由于我不知所措,因此我对如何正确重写此方法的任何见解都将受到高度赞赏。

public class TestNew
{
    public static void main(String []args)
    {
          Rectangle2 one = new Rectangle2(5, 20);
          Box2 two = new Box2(4, 4, 4);
          Box2 three = new Box2(4, 10, 5);
          Cube2 four = new Cube2(4);

          showEffectBoth(one);
          showEffectBoth(two);
          showEffectBoth(three);
          showEffectBoth(four);
          System.out.println(equals(two, four));
    }

    public static void showEffectBoth(Rectangle2 r)
    {
        System.out.println(r);
    }
}

public class Rectangle2
{
    // instance variables
    private int length;
    private int width;

    /**
     * Constructor for objects of class rectangle
     */
    public Rectangle2(int l, int w)
    {
        // initialise instance variables
        length = l;
        width = w;
    }

    public int getLength()
    {
        return length;
    }

    public int getWidth()
    {
        return width;
    }

    public String toString()
    {
        return "Rectangle - " + length + " X " + width;
    }
}

public class Box2 extends Rectangle2
{
    // instance variables
    private int height;

    /**
     * Constructor for objects of class box
     */
    public Box2(int l, int w, int h)
    {
        // call superclass
        super(l, w);
        // initialise instance variables
        height = h;
    }

    public int getHeight()
    {
        return height;
    }

    public String equals(Box2 o, Cube2 p)
    {
        if(o.getLength() + o.getWidth() + o.getHeight() ==  p.getLength() * 3)
        {
            return o.toString() + " is the same size as " + p.toString();
        }
        else
        {
            return o.toString() + " is not the same size as " + p.toString();
        }
    }

    public String toString()
    {
        return "Box - " + getLength() + " X " + getWidth() + " X " + height;
    }
}

public class Cube2 extends Box2
{
    /**
     * Constructor for objects of class Cube.
     */
    public Cube2(int l)
    {
        // call superclass
        super(l, l, l);
    }

    public String toString()
    {
        return "Cube - " + getLength() + " X " + getLength() + " X " + getLength();
    }
}

Well, there is no static method named equals() and taking 2 arguments in your TestNew class. 好吧,在您的TestNew类中没有名为equals()TestNew 2个参数的静态方法。 That's what the compiler tells you. 那就是编译器告诉您的。 To be able to call a method, the method must exist. 为了能够调用方法,该方法必须存在。

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

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