java error: constructor in class cannot be applied
java error: constructor in class cannot be applied
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");
}
}
错误:classWaterHeater 中的构造函数 WaterHeater 无法应用于给定类型。
要求:双倍,双倍,双倍,双倍,双倍
发现:没有参数
原因:实际和正式的参数列表长度不同
使用 Kettle
作为变量名 和 class
的名称将无法正常工作。
一个有用的约定(Java 程序员遵循的约定)是对变量名使用驼峰式命名(注意小写 c),对 class 个名称使用驼峰式命名。
所以保持 class 名称不变,但重命名变量:
WaterHeater kettle = new WaterHeater(...
由于您在 WaterHeater
中编写了构造函数,因此如果不为 Kettle
编写调用 WaterHeater
构造函数的构造函数,就无法扩展 WaterHeater
。您可以按如下方式进行:
public Kettle(double water, double kilowatts, double joules, double temp, double jkg) {
super(water, kilowatts, joules, temp, jkg);
}
另一方面,如果您只是希望 Kettle
成为测试 WaterHeater
的 class,那么您只需要删除单词 extends WaterHeater
。
代码有很多约定错误,但是您尝试使用的代码存在问题 运行 是这需要添加到 Kettle class
public Kettle(double Water, double Kilowatts, double Joules, double Temp, double Jkg) {
super(Water, Kilowatts, Joules, Temp, Jkg);
}
其他指针
对属性使用 camelcase/lowercase 和私有而不是 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;
public double Kilowatts() {
return Kilowatts;
}
应该是
public double getKilowatts() {
return kilowatts;
}
public void setKilowatts(final String kilowatts) {
this.kilowatts = kilowatts;
}
主要 class 也会像这样
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");
}
}
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");
}
}
错误:classWaterHeater 中的构造函数 WaterHeater 无法应用于给定类型。 要求:双倍,双倍,双倍,双倍,双倍 发现:没有参数 原因:实际和正式的参数列表长度不同
使用 Kettle
作为变量名 和 class
的名称将无法正常工作。
一个有用的约定(Java 程序员遵循的约定)是对变量名使用驼峰式命名(注意小写 c),对 class 个名称使用驼峰式命名。
所以保持 class 名称不变,但重命名变量:
WaterHeater kettle = new WaterHeater(...
由于您在 WaterHeater
中编写了构造函数,因此如果不为 Kettle
编写调用 WaterHeater
构造函数的构造函数,就无法扩展 WaterHeater
。您可以按如下方式进行:
public Kettle(double water, double kilowatts, double joules, double temp, double jkg) {
super(water, kilowatts, joules, temp, jkg);
}
另一方面,如果您只是希望 Kettle
成为测试 WaterHeater
的 class,那么您只需要删除单词 extends WaterHeater
。
代码有很多约定错误,但是您尝试使用的代码存在问题 运行 是这需要添加到 Kettle class
public Kettle(double Water, double Kilowatts, double Joules, double Temp, double Jkg) {
super(Water, Kilowatts, Joules, Temp, Jkg);
}
其他指针
对属性使用 camelcase/lowercase 和私有而不是 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;
public double Kilowatts() {
return Kilowatts;
}
应该是
public double getKilowatts() {
return kilowatts;
}
public void setKilowatts(final String kilowatts) {
this.kilowatts = kilowatts;
}
主要 class 也会像这样
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");
}
}