类中的Java构造函数

时间:2015-12-22 19:43:00

标签: java

我一直收到错误

  

类产品中的构造函数产品不能应用于给定类型

super(name, price, barcode)部分。

这是我的代码:

产品类别:

public class Product {


    public String name;
    public float price;
    public int barCode;



  public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

     public float getPrice() {
        return price;
    }
    public void setPrice(float price) {
        this.price = price;
    }

     public int getBarCode() {
        return barCode;
    }
    public void setBarCode(int barCode) {
        this.barCode = barCode;
    }



    public String tostring(){
        return (" Name: " +name+ ",  Price: " +price+ ", Barcode no: " +barCode);
    }

}

巧克力类:

public class Chocolate extends Product{

    public Chocolate(String name, float price, int barcode) {
        super(name, price, barcode);
    }

    @Override
    public String toString() {
        return "Decs : Name :"+super.getName()+", price :"+super.getPrice()+", barcode : "+super.getBarCode();
    }
}

呜咽班:

public class Whine  extends Product {

    public Whine(String name, float price, int barcode) {
        super(name, price, barcode);
    }

    @Override
    public String toString() {
        return "Decs : Name :"+super.getName()+", price :"+super.getPrice()+", barcode : "+super.getBarCode();
    }

}

主要方法:

public static void main(String[] args) {
        Chocolate c1 = new Chocolate("abcd1", "c1", 23.7d, 2.3d);
        Chocolate c2 = new Chocolate("abcd2", "c2", 23d, 2.3d);
        Chocolate c3 = new Chocolate("abcd3", "c3", 23.2d, 2.3d);
        Chocolate c4 = new Chocolate("abcd4", "c4", 23.1d, 2.3d);

        Whine w1 =  new Whine("Whine1", "w1", 23.7d, 2.3d);
        Whine w2 =  new Whine("Whine2", "w2", 233.7d, 22.3d);

        System.out.println("C1"+c1);
        System.out.println("C2"+c2);
        System.out.println("C3"+c3);
        System.out.println("C4"+c4);

        System.out.println("W1"+w1);
        System.out.println("W2"+w2);
    }

}

4 个答案:

答案 0 :(得分:3)

好吧,Product没有你正在调用的构造函数。

答案 1 :(得分:3)

您需要在Product类上创建构造函数:

Product(String name, float price, int barcode) {
    //add your code here
}

因为Java会创建一个像这样的默认构造函数:

Product() {
    //add your code here
}

然后由于两个构造函数的参数长度而导致冲突。

要解决此问题,您需要:

  1. 按照我上面的描述创建自己的构造函数
    1. 调用super()来调用默认的Java构造函数而不是super(name, price, barcode)
    2. 正如@chrylis在他的comment中所说,你压倒tostring()方法,所以你应该在它上方添加一个@Override标签,这样你和其他人就更容易阅读,就像你在Chocolate班上所做的那样:

      @Override
      public String tostring(){
          return (" Name: " +name+ ",  Price: " +price+ ", Barcode no: " +barCode);
      }
      

答案 2 :(得分:2)

您正在调用子类super(name, price, barcode);。在java中,这会尝试调用父类的构造函数,该构造函数除了这三个不存在的参数之外。

您只需要使用默认的超级构造函数(super())或将构造函数添加到Product(这是更好的):

public Product(String name, float price, int barcode) {
  this.name = name;
  this.price = price;
  this.barcode = barcode;
}

答案 3 :(得分:1)

向您的Product类添加适当的构造函数,并将其(可能)声明为抽象类:

public abstract class Product {

/* ... your properties ... */

public Product(String name, float price, int barCode) {
    this.name = name;
    this.price = price;
    this.barCode = barCode;
}

/* etc ... */
}

,更好的想法是将其作为界面:

public interface Product {
    String getName();
    BigDecimal getPrice();
    int getBarCode();
    /* etc */
}
public class Chocolate implements Product {
    private String name;
    private BigDecimal price;
    private int barCode;

    public Chocolate(String name, BigDecimal price, int barCode) {
        this.name = name;
        this.price = price;
        this.barCode = barCode;
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public BigDecimal getPrice() {
        return price;
    }

    @Override
    public int getBarCode() {
        return barCode;
    }
}

然后你可以谈论多态性。

public static class main(String args[]) {
    Product chocolate = new Chocolate("Neuhaus", new BigDecimal(7.38), 9785455);
}