Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法应用父类中的构造函数_Java - Fatal编程技术网

Java 无法应用父类中的构造函数

Java 无法应用父类中的构造函数,java,Java,我试图编写一个程序,该程序采用不同的地毯形状,并使用子类中定义的某些变量创建地毯对象。我的代码是 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){ car

我试图编写一个程序,该程序采用不同的地毯形状,并使用子类中定义的某些变量创建地毯对象。我的代码是

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

我不确定如何修复它。

由于您的超类没有
无参数的默认构造函数,
您需要使用从子类构造函数显式调用您的超类构造函数。这不一定是子类构造函数的第一行

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

}
建议:


遵循Java命名约定,变量名应该是大小写。i、 ,在这种情况下,
id
id
更合适

这很有效,谢谢你。现在,为了弄清楚为什么parseStringToCarpet类不起作用。@user2032938不客气,我没有在你的代码中看到
parseStringToCarpet
,你可能应该删除
super.unitPrice=priceOf
行,因为它是多余的。parseStringToCarpet是一个不同的类,我需要找出CircleCarpet的问题,然后才能尝试修复它。我会的,我只需要再等3分钟,它就会让我恢复。