使用super的java类和子类

时间:2014-04-18 15:37:23

标签: java

我有3个类:class Item:

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;
}





}

和Proccesor课程:

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

4 个答案:

答案 0 :(得分:2)

您无法拨打super(super(..))

你应该只从类Processor调用超类的构造函数,然后它将负责调用Item类的构造函数。

如果你需要绕过硬件的构造函数,那么硬件应该有一个受保护的构造函数,用于调用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);陈述的含义。

更好地缩进代码,很难阅读。

答案 1 :(得分:0)

只需致电super()ProcessorHardware)的超级构造函数将调用Item的构造函数。

答案 2 :(得分:0)

不要使用超级(超级(名称,年份,价格,构造函数)); 只需使用super(名称,年份,价格,构造函数),它将以超级连续方式调用所有其他构造函数

答案 3 :(得分:0)

如上所述,super(super(...不是有效的java语法。现在,考虑一下:

查看您的Hardware构造函数。

仅使用 此构造函数,您明确允许创建Hardware个对象仅提供所有构造函数参数,包括String processor和{{1 }}

现在,因为您的motherboard也是Processor,所以只能通过向其Hardware构造函数提供所有必要信息来构建它。

你可以做什么,例如:

  1. super(...)中添加另一个构造函数,它允许您构造具有较少参数的对象,并从Hardware的构造函数中调用它。

  2. 致电Processor (使用完整的参数列表)在构造super(name, year, price, constructor, proccesor, motherboard, RamMemory, drive)时,可能会为Processor中未使用的参数传递一些默认值,例如Processor