2017-10-05 159 views
0

我在構造函數中遇到這個問題,我希望能夠從供應商類獲取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

回答

1

如果您編輯代碼的格式,你可以清楚地看到你試圖解析字符串"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) { .... } 

在所有情況下的結論是:請別格式化! :)

0

你給在"Supplier1"Supplier2字符串,而構造函數需要對象"Supplier"