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

类和子类
EN

Stack Overflow用户
提问于 2014-04-18 00:27:02
回答 5查看 64关注 0票数 1

好的,我在这里有这些类:

代码语言: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,  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给我这个消息:

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

回答 5

Stack Overflow用户

发布于 2014-04-18 00:28:21

您正在调用一个不存在的超类中的构造函数。

在硬件构造函数中,

代码语言:javascript
复制
super(name);

应更改为调用item中唯一可用的构造函数:

代码语言:javascript
复制
public item(String name,int year,double price,String constructor)

在Java中,调用方法时所有参数都是必需的。不像Javascript这样的语言,如果你不传递一些东西,就会传递一个隐式的undefined

票数 2
EN

Stack Overflow用户

发布于 2014-04-18 00:39:38

当java试图编译hardware.java时,它会看到语句super(name),并在item类中查找一个构造函数,该构造函数将String作为其唯一的参数,因为这是您调用它的方式。

但是,类item只定义了一个构造函数,并且它需要4个参数:

代码语言:javascript
复制
public item(String name,int year,double price,String constructor)

因为item必须指定年份、价格和构造函数,所以您可能需要将这些字段添加到hardware类的构造函数中,然后才能从硬件构造函数方法中调用super(name, year, price, constructor)

或者,您可以修改item类以提供另一个构造函数,省略年份、价格和构造函数参数。

代码语言:javascript
复制
public item(String name){
        this.name = name ; 
    } 

更改之后,来自硬件构造函数的super(name)现在将在类item中找到item(String)构造函数方法。

票数 2
EN

Stack Overflow用户

发布于 2014-04-18 00:28:27

看看item(String name,int year,double price,String constructor)构造函数。你应该打电话给

代码语言:javascript
复制
super(name,year,price,constructor)
// change the parameters as you want but the types should remain

或者向item类添加一个类似这样的构造函数:

代码语言:javascript
复制
item(String name) { ... }

顺便说一下,类名应该(而不是必须)以大写字母开头。

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

https://stackoverflow.com/questions/23138599

复制
相关文章

相似问题

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