I have3类:类项:
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, int year,double price, String constructor, String proccesor,String motherboard, String RamMemory,
String drive) {
super(name, year, price, constructor);
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;
}
public String getProccesor() {
return proccesor;
}
public String getMotherboard() {
return motherboard;
}
public String getRamMemory() {
return RamMemory;
}
public String getDrive() {
return drive;
}
}及班长:
public class Proccesor extends Hardware {
private double ghz;
private int cores;
public Proccesor (String name, int year,double price, String constructor, double ghz, int cores) {
super(super(name,year, price, constructor));
this.ghz= ghz;
this.cores= cores;
}
public void setGhz(double ghz){
this.ghz= ghz;
}
public void setCores(int cores) {
this.cores = cores;
}
public double getGHz() {
return ghz;
}
public int getCores() {
return cores;
}
}我需要将Item类的构造函数调用到Proccesor类中,但我不知道如何做。命令超级(超级(名称、年份、价格、构造函数);给出了错误:
Proccesor.java:7: error: call to super must be first statement in constructor
super(super(name,year, price, constructor));
^
1 error发布于 2014-04-18 15:41:44
你不能打电话给super(super(..))。
您应该只从类处理器调用超类的构造函数,然后它将负责调用Item类的构造函数。
如果您需要绕过硬件的构造函数,那么硬件应该有一个受保护的构造函数,用于调用项构造函数,并且可以由子类调用。
public Hardware(String name, int year,double price, String constructor, String proccesor,String motherboard, String RamMemory, String drive) {
this(name, year, price, constructor);
this.proccesor= proccesor;
this.motherboard= motherboard;
this.RamMemory= RamMemory;
this.drive= drive;
}
protected Hardware(String name, int year,double price, String constructor) {
super(name, year, price, constructor);
}正如你所看到的:
这就是this(name, year, price, constructor);语句的含义。
更好地缩进代码,很难读懂。
发布于 2014-04-18 15:41:33
只要调用super(),Processor (Hardware)的超级构造函数就会调用Item的构造函数。
发布于 2014-04-18 15:41:56
不要使用超级(超级(名称、年份、价格、构造函数);只要使用超级(名称、年份、价格、构造函数),它就会依次调用所有具有超级的构造函数。
https://stackoverflow.com/questions/23157363
复制相似问题