Java Rest构造函数继承

问题描述:

我在构造函数中遇到这个问题,我希望能够从供应商类获取supplierName并将其添加到产品中。Java Rest构造函数继承

public class Product { 

    private long id; 
    private long barcode; 
    private String description; 
    private String zone; 
    private int quantity; 

    Supplier supplierName; 


    public Product(){ 


    } 

    public Product(long id, long barcode, String description, String zone, 
        int quantity, Supplier supplierName) { 
     super(); 
     this.id = id; 
     this.barcode = barcode; 
     this.description = description; 
     this.zone = zone; 
     this.quantity = quantity; 
     this.supplierName = supplierName; 

    } 

但是,当我到达我的产品服务类

public class ProductService { 

    private Map<Long, Product> products = DatabaseClass.getProducts(); 

    public ProductService(){ 
     products.put(1L,new Product(1,1,"Lighter","Gift",5000,"Supplier1")); 
     products.put(2L,new Product(2,2,"Lighter","Gift",3500,"supplier2")); 

    } 

它给我一个错误的supplier1 & supplier2问题所在。

任何帮助将appriciated。

+2

它给你什么错误? –

+0

如果您可以将您收到的错误添加到您的问题中,它会很有帮助 - 它应该包括发生问题的行号 –

+0

删除与“产品”匹配的论题 – college1

如果您编辑代码的格式,你可以清楚地看到你试图解析字符串"Supplier1""supplier2"的构造函数,它接受作为对象类型的Supplier

如果你有一个定义的类Supplier,构造函数调用更改为:

products.put(2L,new Product(2,2, "Lighter","Gift",3500,new Supplier(...))); 

或者,如果供应商应该是字符串,改变其声明和构造函数。

private String supplier; 

public Product(long id, long barcode, String description, String zone, int quantity, String supplier) { .... } 

在所有情况下的结论是:请别格式化! :)

你给在"Supplier1"Supplier2字符串,而构造函数需要对象"Supplier"