Java 添加 ArrayList 的方法未定义类型 <OBJECT>
Java Method add ArrayList is undefined for the type <OBJECT>
我正在为我的家庭作业构建一个 java 程序,我必须在其中向特定商店添加产品。尝试从 Store class.
添加到 ArrayList 时遇到问题
我有 class 个产品如下:
class Product {
private String pName;
private int pPrice;
private int pQty;
public Product (String pName, int pPrice, int pQty) {
this.pName = pName;
this.pPrice = pPrice;
this.pQty = pQty;
}
}
和class存储如下:
class Store {
private String storeName;
ArrayList<Product> pList =new ArrayList<>();
public Store() {
String name = storeName;
pList = new ArrayList<Product>();
}
public Store(String newStoreName,ArrayList<Product> newPList) {
this.storeName = newStoreName;
this.pList = newPList;
}
void setName(String storeName) {
this.storeName = storeName;
}
void setProduct(Product pList) {
pList.add(this.pList);//This return method add undefined for type Product, how to solve this error?
}
String getName() {
return storeName;
}
ArrayList<Product> getProductList() {
return pList;
}
}
void setProduct(Product pList) {
pList.add(this.pList);//This return method add undefined for type Product, how to solve this error?
}
应该是
void addProduct(Product product) {
pList.add(product);
}
1 - 你应该像下面这样改变你的构造函数:_-
public Store(String newStoreName,ArrayList<Product> newPList) {
this.storeName = newStoreName;
pList.addAll(newPList);// This is standard and recommended way to add all element in list.
}
2 - 更改您的 setProduct 方法。这个运算符不是这样工作的。
void setProduct(Product pList) {
pList.add(pList);
}
我正在为我的家庭作业构建一个 java 程序,我必须在其中向特定商店添加产品。尝试从 Store class.
添加到 ArrayList 时遇到问题我有 class 个产品如下:
class Product {
private String pName;
private int pPrice;
private int pQty;
public Product (String pName, int pPrice, int pQty) {
this.pName = pName;
this.pPrice = pPrice;
this.pQty = pQty;
}
}
和class存储如下:
class Store {
private String storeName;
ArrayList<Product> pList =new ArrayList<>();
public Store() {
String name = storeName;
pList = new ArrayList<Product>();
}
public Store(String newStoreName,ArrayList<Product> newPList) {
this.storeName = newStoreName;
this.pList = newPList;
}
void setName(String storeName) {
this.storeName = storeName;
}
void setProduct(Product pList) {
pList.add(this.pList);//This return method add undefined for type Product, how to solve this error?
}
String getName() {
return storeName;
}
ArrayList<Product> getProductList() {
return pList;
}
}
void setProduct(Product pList) {
pList.add(this.pList);//This return method add undefined for type Product, how to solve this error?
}
应该是
void addProduct(Product product) {
pList.add(product);
}
1 - 你应该像下面这样改变你的构造函数:_-
public Store(String newStoreName,ArrayList<Product> newPList) {
this.storeName = newStoreName;
pList.addAll(newPList);// This is standard and recommended way to add all element in list.
}
2 - 更改您的 setProduct 方法。这个运算符不是这样工作的。
void setProduct(Product pList) {
pList.add(pList);
}