我在我的构造函数中遇到这个问题,我希望能够从供应商类中获取supplierName并将其添加到产品中。
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
的错误。
如有任何帮助,我们将不胜感激。
最佳答案
如果您编辑代码的格式,您可以清楚地看到您尝试将字符串 "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) { .... }
<小时/>
所有情况的结论都是:请进行格式化!:)
关于Java Rest 构造函数继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46585314/