简体   繁体   English

类和子类

[英]Class and subclass

Ok i have these classes here : 好的,我在这里有这些课:

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 ; 

    }
}

and also class hardware: 以及类硬件:

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





}

Now when i compile item, it compiles fine and no problems pop up.But when i try to compile the hardware class, cmd gives me this message: 现在,当我编译项目时,它可以正常编译并且不会弹出任何问题。但是当我尝试编译硬件类时,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

You're calling a constructor in the superclass that does not exist. 您正在不存在的超类中调用构造函数。

In your hardware constructor, 在您的硬件构造器中,

super(name);

Should be changed to call the only available constructor in item: 应该更改为调用item中唯一可用的构造函数:

public item(String name,int year,double price,String constructor)

In Java, all arguments are required when calling a method. 在Java中,调用方法时需要所有参数。 Unlike a language like Javascript, where if you don't pass something, an implicit undefined is passed. 与Javascript之类的语言不同,在Java语言中,如果您不传递内容,则传递隐式undefined

When java attempts to compile hardware.java it sees the statement super(name) and looks for a constructor in class item which takes a String as its only argument, because that is how you are calling it. 当Java尝试编译hardware.java它将看到语句super(name)并在类item寻找一个构造函数,该构造函数将String作为其唯一参数,因为这就是调用它的方式。

However, the class item only has one constructor defined, and it expects 4 arguments: 但是,该类item仅定义了一个构造函数,并且需要4个参数:

public item(String name,int year,double price,String constructor)

Because an item must have a year, price, and contructor specified, you may need to add those fields to the constructor for the hardware class, and then you could call super(name, year, price, constructor) from your hardware constructor method. 由于item必须指定年份,价格和构造函数,因此您可能需要将这些字段添加到hardware类的构造函数中,然后可以从硬件构造函数方法中调用super(name, year, price, constructor)

Alternatively, you could modify the item class to also provide another constructor, omitting the year, price, and constructor argument. 或者,您可以修改item类以提供另一个构造函数,而省略year,price和构造函数参数。

public item(String name){
        this.name = name ; 
    } 

After that change, super(name) from the hardware constructor will now find the item(String) constructor method in class item. 进行此更改之后,硬件构造函数中的super(name)现在将在类item中找到item(String)构造函数方法。

look at the item(String name,int year,double price,String constructor) constructor. 查看item(String name,int year,double price,String constructor)构造函数。 you should call 你应该打电话

super(name,year,price,constructor)
// change the parameters as you want but the types should remain

or add a constructor like this to the item class: 或将类似的构造函数添加到item类:

item(String name) { ... }

BTW class names should (not must) start with capital letters. BTW类名称应该(不是必须)以大写字母开头。

class item没有只接受String参数的构造函数。

Item class没有item(String name)构造函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM