1

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

    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 唯一的问题是我没有得到那个输出。equals()当我equals()为 Cube 类做方法时,我也可以只从 Box2 类继承方法吗?

4

3 回答 3

1

每当您通过 a不是a 时,您对Cubeinside of 的强制转换Box2.equals()将失败并抛出a 。您在整个代码中重复此错误。ClassCastExceptionBox2Cube

我建议修复if语句的大括号,尽管它应该可以按预期工作。

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

此外,在内部Cube.equals(),您应该强制b转换为 a Cube,并从声明的Cube对象调用每个函数。你应该用几乎任何equals(Object)方法做同样的事情。

在您的实现中,因为Rectangle2andCube不要覆盖getWidth()or getLength(), t.getWidth()andc.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

于 2013-03-20T19:14:56.493 回答
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);
  ... 
于 2013-03-20T19:11:43.167 回答
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;
    }
}

如果要打印此方法的结果,请将值分配给任何变量,打印并返回该值。

于 2013-03-20T19:22:19.937 回答