2017-10-03 5 views
0

このテストに失敗するのはなぜですか?別のクラスから拡張されたクラスに変数を設定する必要があります。または、タイプを無効にする

テストキューブ(1.0、1.0、1.0)1.0 テストフィードバックに "キューブ" のタイプ、

幅、長さ及び高さをそれぞれ設定

期待:キューブ、1.0、1.0、1.0

あなたのタイプ:Rectangle、1.0,1.0,1.0

キューブにはそのタイプをキューブに設定する必要があります。今は、それ自体がRectangleに設定されているようです。私のメインはShapeのArrayを塗りつぶしていますが、次に各シェイプのTypeに応じて異なるタイプのシェイプを数えるメソッドがあります。私の矩形クラスが既にシェイプを拡張しているときに、Cubeをシェイプとして定義するにはどうすればよいですか?私はCubeクラスを長方形に拡張する必要があります。それは私がエリアと長さと幅にアクセスできる必要があるからです。

私はまた、私のキューブによって実装されている非常に単純なインターフェイスを持っています。音量を見つけるだけです。私は何とかインターフェイスを利用して型をオーバーライドできますか?

この特定の質問に対するStackOverflowの回答が見つかりません。ここで

は私のRectangleクラスは、ここで

public class Rectangle extends Shape { 
    protected double width; 
    protected double length; 

    public Rectangle(double width, double length) { 
     super("Rectangle"); 
     setWidth(width); 
     setLength(length); 
    }// end ctr 

    public final double getWidth () {return width; } 
    public final double getLength() {return length;} 

    public final void setWidth (double width) { 
     if (width < 0) { 
      System.out.println("Value could not be updated due to negative double."); 
     }else 
      this.width = width; 
    }// end width setter 

    public final void setLength (double length) { 
     if (length < 0) { 
      System.out.println("Value could not be updated due to negative double."); 
     }else 
      this.length = length; 
    }// end length setter 

    @Override 
    public double area() { 
     return length * width; 
    }// end area method 

    public double perimeter() { 
     return 2 * (length + width); 
    }// end perimeter method 

    @Override 
    public String toString() { 
     String str = ""; 

     str += String.format("%10s", "Rectangle:") + " "; 
     str += "width: " + String.format("%.1f", width) + ", " + "length: " + String.format("%.1f", length); 
     str += ", " + "area: " + String.format("%.2f", area()) + ", "; 
     str += "perimeter: " + String.format("%.2f", perimeter()); 

     return str; 
    }// end descriptor 

}// end rect class 

あるRectangleクラスは、私のShapeクラスから来て、私のキューブクラスは

​​

で、

public abstract class Shape { 
    protected String type; 

    public Shape (String type) { 
     setType(type); 
    }// end ctr 

    public final String getType()   {return type;  } 
    public final void setType (String type) {this.type = type;} 

    public abstract double area(); // end prototype 

    public double getArea() {return area();} 

    @Override 
    public String toString() { 
     String str = ""; 

     str += type; 

     return str; 
    }// end descriptor 

}// end shape class 

答えて

1

あなたのキューブクラスにスーパーを使用していますコール矩形のコンストラクタは、 Shapeクラスのコンストラクタを呼び出します。 "Rectangle"という文字列を基本的に持っているので、キューブのコンストラクタではキューブのタイプを設定することはできません。明示的にsetTypeメソッドを使用する必要があります。

あなたは、キューブのコンストラクタで行を追加することができ

this.setType("Cube"); 

、それ(テストしていない)動作するはずです。

+0

あなたは聖人です。私はこれに何時間も執着している。 今はあまりにも明白なように見えますが、どのように見落としましたか? Gahh .... だからこそ、とても感謝しています。ありがとうございます。 –

関連する問題