以下のためにCube
クラス、私はエラーを取り除くためにしようとしています:
Cube.java:12: error: constructor Rectangle in class Rectangle cannot be applied to given types;
super(x, y);
^
required: int,int,double,double
found: int,int.......
キューブの各面が長方形であり、その長さと幅がキューブの辺と同じである必要があることを知っていますが、長さと幅を同じにするためにRectangleコンストラクターに何を渡す必要があるのかわかりません。キューブの側面。
また、長方形の面積にキューブの辺の長さを掛けた体積を計算しようとしています
これはCubeクラスです
// ---------------------------------
// File Description:
// Defines a Cube
// ---------------------------------
public class Cube extends Rectangle
{
public Cube(int x, int y, int side)
{
super(x, y);
side = super.area(); // not sure if this is right
}
public int getSide() {return side;}
public double area() {return 6 * super.area();}
public double volume() {return super.area() * side;}
public String toString() {return super.toString();}
}
これは長方形クラスです
// ---------------------------------
// File Description:
// Defines a Rectangle
// ---------------------------------
public class Rectangle extends Point
{
private int x, y; // Coordinates of the Point
private double length, width;
public Rectangle(int x, int y, double l, double w)
{
super(x, y);
length = l;
width = w;
}
public int getX() {return x;}
public int getY() {return y;}
public double getLength() {return length;}
public double getWidth() {return width;}
public double area() {return length * width;}
public String toString() {return "[" + x + ", " + y + "]" + " Length = " + length + " Width = " + width;}
}
このオブジェクトの構築そのものが、拡張の概念を見逃しているようです。
A Cube
はではありませんRectangle
。は、向きの空間データを含むCube
複数Rectangle
のの複合と考えることができますが、長方形はのメンバー(読み取り属性/フィールド)である必要がありCube
ます。
この点を説明するために、次のステートメントの違いを検討してください。
すべての立方体は長方形です。
すべての猫は動物です。
Cat
スーパークラスを拡張するオブジェクトを作成することもできますAnimal
。Cube
とRectangle
はこの関係を共有していません。
コードを次のようにリファクタリングすることを検討してください:
public class Cube {
private List<Rectangle> faces:
....
}
これをさらに一歩進めるために、すべてRectangle
のがであるとは限りませんPoint
。
ポイントは、x、y座標の1つのペアです。正確に描画するためにRectangle
必要な情報の最小量は2 Point
秒です。
見る
+--
| |
--+
反対側のコーナーPoint
(ここでは+でマークされています)がある場合は、を描くことができますRectangle
。
これを踏まえると、おそらくsのRectangle
ペアをPoint
メンバーとして持つようにリファクタリングする必要もあります。
何かのようなもの:
public class Rectangle {
private Point firstCorner;
private Point secondCorner;
...
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加