2

さまざまな形のカーペットを取り、サブクラスで定義された特定の変数を使用してカーペット オブジェクトを作成するプログラムをコーディングしようとしています。私のコードは

public abstract class Carpet{

protected int area = 0;
protected double unitPrice = 0;
protected double totalPrice = 0.0;
protected String carpetID;

public Carpet(String id, double thisPrice){
    carpetID = id;
    unitPrice = thisPrice;
}

public String getCarpetId(){
    return carpetID;
}

public String toString(){
    String carpet = new String("\n" + "The CarpetId:\t\t" + getCarpetId() + "\nThe Area:\t\t" + area + "\nThe Unit Price\t\t" + unitPrice + "\nThe Total Price\t" + totalPrice + "\n\n");
    return carpet;
}

public abstract void computeTotalPrice();

}

サブクラスは

public class CircleCarpet extends Carpet{

private int radius;

public CircleCarpet(String id, double priceOf, int rad){
    radius = rad;
    super.unitPrice = priceOf;
    computeTotalPrice();

}

public void computeTotalPrice(){
    super.area = radius * radius * 3;
    super.totalPrice = area * unitPrice;
}


public String toString(){
    String forThis = new String("\nThe Carpet Shape:\tCircle\nThe radius:\t\t" + radius + "\n");
    return forThis + super.toString();
}

}

しかし、サブクラスをコンパイルしようとするたびにエラーが発生します

11: error: constructor Carpet in class Carpet cannot be applied to given types;
public CircleCarpet(String ID, double priceOf, int rad){
                                                       ^


 required: String,double
  found: no arguments
  reason: actual and formal argument lists differ in length
1 error

Tool completed with exit code 1

それを修正するために何をすべきかわかりません。

4

1 に答える 1

9

スーパークラスには がないため、super()no-args default constructor,を使用してサブクラスコンストラクターからスーパークラスコンストラクターを明示的に呼び出す必要があります。これは、サブクラスコンストラクターの最初の行である必要はありません。

 public CircleCarpet(String ID, double priceOf, int rad){
    super(ID, priceOf)
    radius = rad;
    super.unitPrice = priceOf;
    computeTotalPrice();

}

アドバイス:

Java 命名規則に従い、変数名はキャメルケースにする必要があります。i.、この場合idは よりも適切ですID

于 2013-02-01T15:14:20.637 に答える