简体   繁体   English

当我尝试调用超级构造函数时,复制构造函数生成错误

[英]Copy constructor is generating an error when I try to call the super constructor

When writing the copy constructor for my class I get an error message, what am I doing wrong?为我的 class 编写复制构造函数时,我收到一条错误消息,我做错了什么?

public class SaleItem extends Item {
double price;
double shippingCost;
int numItems;


public SaleItem(SaleItem toCopy){

    this.price = toCopy.price;
    this.shippingCost = toCopy.shippingCost;
    this.numItems = toCopy.numItems;
}
public SaleItem(String name, String description, double price, double shippingCost, int numItems){
   super(name, description);
    this.price = price;
    this.shippingCost= shippingCost;
    this.numItems = numItems;
}

You should declare that attributes as protected , for instance:您应该将该属性声明为protected ,例如:

public class Item {

    protected String description;
    protected String name;

    public Item(){}

    public Item(String description, String name){
        this.description = description;
        this.name = name;
    }
    //Getter and setters...

}

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

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