好的,我在这里有这些类:
public class item {
public String name,constructor ;
public int year;
public double price ;
public item(String name,int year,double price,String constructor){
this.name = name ;
this.year = year ;
this.price = price ;
this.constructor = constructor ;
}
public String getName(){
return name ;
}
public int getYear(){
return year ;
}
public double getPrice(){
return price ;
}
public String getConstructor(){
return constructor ;
}
public void setName(String name){
this.name = name ;
}
public void setYear(int year ){
this.year = year ;
}
public void setPrice(double price){
this.price = price ;
}
public void setConstructor(String constructor){
this.constructor = constructor ;
}
}同时也是硬件类:
public class hardware extends item {
private String proccesor;
private String motherboard;
private String RamMemory;
private String drive;
public hardware(String name, String proccesor,String motherboard, String RamMemory,
String drive ) {
super(name);
this.proccesor= proccesor;
this.motherboard= motherboard;
this.RamMemory= RamMemory;
this.drive= drive;
}
public void setProccesor (String proccesor) {
this.proccesor= proccesor;
}
public void setMotherboard(String motherboard){
this.motherboard= motherboard;
}
public void setRam(String RamMemory){
this.RamMemory= RamMemory;
}
public void setDrive(String drive){
this.drive= drive;
}
}现在,当我编译项目时,它编译得很好,没有问题弹出up.But当我尝试编译硬件类时,cmd给我这个消息:
Hardware.java.11: error: constructor item in class item cannot be applied to given types super(name);
Required :String,int,double,String
Found: String
Reason: actual and formal arguments lists differ in length发布于 2014-04-18 00:28:21
您正在调用一个不存在的超类中的构造函数。
在硬件构造函数中,
super(name);应更改为调用item中唯一可用的构造函数:
public item(String name,int year,double price,String constructor)在Java中,调用方法时所有参数都是必需的。不像Javascript这样的语言,如果你不传递一些东西,就会传递一个隐式的undefined。
发布于 2014-04-18 00:39:38
当java试图编译hardware.java时,它会看到语句super(name),并在item类中查找一个构造函数,该构造函数将String作为其唯一的参数,因为这是您调用它的方式。
但是,类item只定义了一个构造函数,并且它需要4个参数:
public item(String name,int year,double price,String constructor)因为item必须指定年份、价格和构造函数,所以您可能需要将这些字段添加到hardware类的构造函数中,然后才能从硬件构造函数方法中调用super(name, year, price, constructor)。
或者,您可以修改item类以提供另一个构造函数,省略年份、价格和构造函数参数。
public item(String name){
this.name = name ;
} 更改之后,来自硬件构造函数的super(name)现在将在类item中找到item(String)构造函数方法。
发布于 2014-04-18 00:28:27
看看item(String name,int year,double price,String constructor)构造函数。你应该打电话给
super(name,year,price,constructor)
// change the parameters as you want but the types should remain或者向item类添加一个类似这样的构造函数:
item(String name) { ... }顺便说一下,类名应该(而不是必须)以大写字母开头。
https://stackoverflow.com/questions/23138599
复制相似问题