java错误:类中的构造函数无法应用

时间:2015-01-13 14:19:05

标签: java constructor

public class WaterHeater{

    public double Kilowatts;
    public double Water;
    public double Joules;
    public double Temp;
    public double Jkg;
    public double Energy;
    public double Time;

  //Constructor method

  public WaterHeater (double Water, double Kilowatts, double Joules, double Temp, double Jkg)  {

    this.Water = Water;
    this.Kilowatts = Kilowatts;
    this.Joules = Joules;
    this.Temp = Temp;
    this.Jkg = Jkg;
  }

    //Accessor for Kilowatts
    public double Kilowatts(){
    return Kilowatts;
    }

    //Accessor for Water
    public double Water(){
    return Water;
    }

    //Method for Energy used
    public double Energy(){
    Energy = Water*Jkg*Temp;
    return Energy;
    }

    //Method for Time to boil
    public double Time(){
    Time = Energy/Kilowatts;
    return Time;
    }
    }

//Test class

 public class Kettle extends WaterHeater{

  public static void main( String args[] ) 
  {

    WaterHeater Kettle = new WaterHeater(1, 2, 3600, 70, 4200); 

    System.out.println("\nWater: " +Kettle.Water + " L" +
    "\nKilowatts: " + Kettle.Kilowatts + " KW"+
    "\nEnergy used: " +Kettle.Energy() + " J" +
    "\nTime to boil: " +Kettle.Time() + " Seconds");


  }

}

错误:WaterHeater类中的构造函数WaterHeater无法应用于给定类型。        必需:双,双,双,双,双        发现:没有争论        原因:实际和正式的arqument列表长度不同

3 个答案:

答案 0 :(得分:3)

代码有很多惯例,但是你试图运行代码的问题是这需要添加到Kettle类

public Kettle(double Water, double Kilowatts, double Joules, double Temp, double Jkg) {
    super(Water, Kilowatts, Joules, Temp, Jkg);     
}

其他指示

使用camelcase / lowercase作为属性以及private而不是public

public double Kilowatts;
public double Water;
public double Joules;
public double Temp;
public double Jkg;
public double Energy;
public double Time;

应该是

private double kilowatts;
private double water;
private double joules;
private double temp;
private double jkg;
private double energy;
private double time;

使用Accessors and Mutators

public double Kilowatts() {
    return Kilowatts;
}

应该是

public double getKilowatts() {
    return kilowatts;
}
public void setKilowatts(final String kilowatts) {
    this.kilowatts = kilowatts;
}

主要类也看起来像这样

public class Kettle extends WaterHeater {

    public Kettle(double water, double kilowatts, double joules, double temp, double jkg) {
        super(water, kilowatts, joules, temp, jkg);     
    }

    public static void main(String args[]) {

        WaterHeater kettle = new WaterHeater(1, 2, 3600, 70, 4200);

        System.out.println("\nWater: " + kettle.getWater() + " L" + "\nKilowatts: " + kettle.getKilowatts() + " KW" + "\nEnergy used: " + kettle.getEnergy() + " J"
            + "\nTime to boil: " + kettle.getTime() + " Seconds");
    }
}

答案 1 :(得分:2)

由于您已在WaterHeater中编写了构造函数,因此如果不编写调用WaterHeater构造函数的Kettle构造函数,则无法扩展WaterHeater。你可以这样做:

public Kettle(double water, double kilowatts, double joules, double temp, double jkg) {
    super(water, kilowatts, joules, temp, jkg);
}

另一方面,如果您希望Kettle成为测试WaterHeater的班级,那么您只需删除extends WaterHeater字样。

答案 2 :(得分:0)

使用Kettle作为变量名称​​和 class的名称无法正常运作。

一个有用的约定(以及Java程序员遵循的约定)是将camelCase用于变量名(注意小写c)和CamelCase用于类名。

因此保持类名不变,但重命名变量:

WaterHeater kettle = new WaterHeater(...