首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >类和子类使用超级

类和子类使用超级
EN

Stack Overflow用户
提问于 2014-04-18 15:37:23
回答 4查看 68关注 0票数 0

I have3类:类项:

代码语言:javascript
复制
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 ; 

    }
}

类硬件:

代码语言:javascript
复制
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;
}





}

及班长:

代码语言:javascript
复制
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类中,但我不知道如何做。命令超级(超级(名称、年份、价格、构造函数);给出了错误:

代码语言:javascript
复制
Proccesor.java:7: error: call to super must be first statement in constructor
super(super(name,year, price, constructor));
           ^
1 error
EN

回答 4

Stack Overflow用户

发布于 2014-04-18 15:41:44

你不能打电话给super(super(..))

您应该只从类处理器调用超类的构造函数,然后它将负责调用Item类的构造函数。

如果您需要绕过硬件的构造函数,那么硬件应该有一个受保护的构造函数,用于调用项构造函数,并且可以由子类调用。

代码语言:javascript
复制
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);语句的含义。

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

票数 2
EN

Stack Overflow用户

发布于 2014-04-18 15:41:33

只要调用super()Processor (Hardware)的超级构造函数就会调用Item的构造函数。

票数 0
EN

Stack Overflow用户

发布于 2014-04-18 15:41:56

不要使用超级(超级(名称、年份、价格、构造函数);只要使用超级(名称、年份、价格、构造函数),它就会依次调用所有具有超级的构造函数。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23157363

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档