2013-03-20 55 views
1

我有一個類層次結構,低於顯示:類對象比較 - 獲取錯誤輸出equals方法

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()方法嗎?

+2

我不明白。當你在所有的代碼中沒有一個'print' /'println'時,你希望你的程序能夠打印'...與......相同的大小? – us2012 2013-03-20 18:57:40

+1

你爲什麼要投射一個Rectangle2到它的sublcass實例?誰這樣做,爲什麼? – ITroubs 2013-03-20 18:58:39

+0

@ us2012我試圖把一個println放在equals類中,但它不會讓我。 – user2059140 2013-03-20 19:00:04

回答

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); 
    ... 
+1

你在哪裏看到無法訪問的代碼? – maszter 2013-03-20 19:13:39

+0

我沒有。但編譯器的一種方式不會讓他把一個打印將是這個。 – 2013-03-20 19:16:37

1

你投以CubeBox2.equals()內將失敗,並拋出一個ClassCastException每當你傳遞一個Box2,是不是也是一種Cube。您在整個代碼中重複出現此錯誤。

我建議修復if語句的大括號,但它應該按預期工作。

實際上,我不希望你得到任何輸出。您的代碼中沒有任何print()println()調用。

另外,在Cube.equals()中,您應該將b投射到Cube,並從聲明的Cube對象中調用每個函數。你應該在幾乎所有的方法中都這樣做。

在您的實現,而且,由於Rectangle2Cube不會覆蓋getWidth()getLength(),​​3210和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,因爲它們是不可變的。最後,如果您有其他具有相似名稱的類,則應該找到更有意義的方法來區分類名,而不是數字。否則,請從名稱Rectangle2Box2中刪除2

+0

立方體只有一個維度,你不需要比較長度,寬度和高度,其中一個是足夠的 – maszter 2013-03-20 19:36:40

+0

當然,但我希望編譯器爲你處理。我沒有太多偏好,所以我會改變它來證明它是可能的。 – stoooops 2013-03-20 19:38:50

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

如果你要打印這種方法的結果,將值分配給任何變量,打印和返回值。