类对象比较 - 为equals方法获取错误的输出

时间:2013-03-20 18:54:06

标签: java class object

我有一个类层次结构,如下所示:

    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;
        }
        // return the height
        public int getLength()
        {
            return length;
        }
        public int getWidth()
        {
            return width;
        }
        public String toString()
        {
            return "Rectangle - " + length + " X " + width;
        }
public boolean equals( Object b )
           {
               if ( ! (b instanceof Rectangle2) )
               return false;
               Box2 t = (Box2)b;
               Cube c = (Cube)b;
               return t.getLength() == getLength() 
                       && t.getWidth() == getWidth() 
                       && c.getLength() == getLength() 
                       && c.getWidth() == getWidth() ;
}

    }

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;
    }
    // return the height
    public int getHeight()
    {
        return height;
    }
    public String toString()
    {
        return "Box - " + getLength() + " X " + getWidth() + " X " + height;
    }
         public boolean equals( Object b )
           {
               if ( ! (b instanceof Box2) )
               return false;
               Rectangle2 t = (Rectangle2)b;
               Cube c = (Cube)b;
               return t.getLength() == getLength() 
                       && t.getWidth() == getWidth()  
                       && c.getLength() == getLength() ;
  } 
   }

public class Cube extends Box2 {
            public Cube(int length)
             {
              super(length, length, length);
             } 
            public String toString()
    {
        return "Cube - " + getLength() + " X " + getWidth() + " X " + getHeight();
    }

             public boolean equals( Object b )
           {
               if ( ! (b instanceof Cube) )
               return false;
               Rectangle2 t = (Rectangle2)b;
               Box2 c = (Box2)b;
               return t.getLength() == getLength() 
                       && t.getWidth() == getWidth()  
                       && c.getLength() == getLength() 
                       && c.getWidth() == getWidth() 
                       && c.getHeight() == getHeight() ;
  }      
}

我创建了equals()方法,这样当一个类的实例与另一个类相等时,它会打印一些类似“此类的维度等于该类的维度”。这是一个例子:http://i.stack.imgur.com/Kyyau.png 唯一的问题是我没有得到那个输出。当我为Cube类执行equals()方法时,我还可以从Box2类继承equals()方法吗?

3 个答案:

答案 0 :(得分:1)

Cube内的Box2.equals()投射会失败,只要你传递的ClassCastException Box2 Cube,你就会投出if。您在整个代码中重复此错误。

我建议修复print()语句的大括号,但它应该按预期工作。

实际上,我不希望你得到任何输出。您的代码中没有println()Cube.equals()次调用。

此外,在b内,您应该将Cube转换为Cube,并从声明的equals(Object)对象中调用每个函数。你应该在几乎任何Rectangle2方法中都这样做。

在您的实施中,由于CubegetWidth()不会覆盖getLength()t.getWidth()c.getWidth()getLength()会调用相同的功能,每次都返回相同的输出。同样适用于Rectangle2

例如,您的public class Rectangle2 { private final int length; private final int width; public Rectangle2(int length, int width) { this.length = length; this.width = width; } private int getLength() { return length; } private int getWidth() { return length; } public String toString() { return "Rectangle - "+length+" X "+width; } @Override public boolean equals(Object o) { if (!(o instanceof Rectangle2)) { return false; } final Rectangle2 r = (Rectangle2) o; return this.getLength() == r.getLength() && this.getWidth() == r.getWidth() && this.getHeight() == r.getHeight(); } } 类看起来应该是这样的。

Box2

并且您的public class Box2 extends Rectangle2 { private final int height; public Box2(int length, int width, int height) { super(length, width); this.height = height; } private int getHeight() { return height; } public String toString() { return "Box - "+length+" X "+width"+ X "+height; } @Override public boolean equals(Object o) { if (!(o instanceof Box2)) { return false; } final Box2 b = (Box2) o; return this.getLength() == b.getLength() && this.getWidth() == b.getWidth() && this.getHeight() == b.getHeight(); } } 类看起来应该是这样的。

Cube

并且您的public class Cube extends Box2 { private final int height; public Cube(int length) { super(length, length, length); } public String toString() { return "Cube - "+length+" X "+width"+ X "+height; } @Override public boolean equals(Object o) { if (!(o instanceof Cube)) { return false; } final Cube c = (Cube) o; return this.getLength() == c.getLength(); // length == width == height } } 类应该看起来像这样

System.out.println()

然后,您应该可以添加对final的调用,以将所需的输出打印到控制台。

您应该将字段声明为2,因为它们是不可变的。

最后,如果您有其他具有相似名称的类,您应该找到更有意义的方法来区分类名而不是数字。否则,请从名称Rectangle2Box2中删除{{1}}。

答案 1 :(得分:0)

您不能在最后return声明之后放置代码。它无法访问。

您需要分配"等于"返回局部变量,打印然后返回:

boolean equal = getLength() == getLength() 
               && t.getWidth() == getWidth()  
               && c.getLength() == getLength() 
               && c.getWidth() == getWidth() 
               && c.getHeight() == getHeight() ;
if (equal) {
    System.out.prinltn("equal");
}

return equal;

equals方法是继承的,但您要覆盖它。如果要在扩展类的主体中调用超类equals方法,则必须在覆盖方法的开头调用super()

public boolean equals( Object b ) {
     boolean equal = super.equals(b);
  ... 

答案 2 :(得分:0)

在我看来,你的equals方法应该是这样的(只是更改类):

public boolean equals( Object b ) {
    if (b instanceof Rectangle2) {
        Rectangle2 rectangle2 = (Rectangle2)b;
        return this.getLength() == rectangle2.getLength()
            && this.getWidth() == rectangle2.getWidth()
            && this.getHeight() == rectangle2.getHeight();
    } else {
        return false;
    }
}

如果要打印此方法的结果,请为任何变量赋值,打印并返回该值。