Java 休息构造函数继承
Java Rest constructor inheritance
我在我的构造函数中遇到了这个问题,我希望能够从供应商那里获取 supplierName class 并将其添加到产品中。
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;
}
但问题出在我获得产品服务时class
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"
如果您编辑代码的格式,您可以清楚地看到您尝试将字符串 "Supplier1"
和 "supplier2"
解析为接受 Supplier
作为对象的构造函数类型。
如果您定义了 class 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) { .... }
所有情况下的结论是:请格式化! :)
我在我的构造函数中遇到了这个问题,我希望能够从供应商那里获取 supplierName class 并将其添加到产品中。
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;
}
但问题出在我获得产品服务时class
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"
如果您编辑代码的格式,您可以清楚地看到您尝试将字符串 "Supplier1"
和 "supplier2"
解析为接受 Supplier
作为对象的构造函数类型。
如果您定义了 class 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) { .... }
所有情况下的结论是:请格式化! :)