Java 将对象添加到将另一个对象作为参数的ArrayList
大家好,我正在努力复习我的java知识,因为我已经有一段时间没有用java编写代码了。我正在做这个商店项目,它有三个等级。1.产品2.库存物品和3.仓库 Product类要求名称为字符串,成本为双精度。有用于设置和检索数据的访问器和mutator方法Java 将对象添加到将另一个对象作为参数的ArrayList,java,object,arraylist,parameters,constructor,Java,Object,Arraylist,Parameters,Constructor,大家好,我正在努力复习我的java知识,因为我已经有一段时间没有用java编写代码了。我正在做这个商店项目,它有三个等级。1.产品2.库存物品和3.仓库 Product类要求名称为字符串,成本为双精度。有用于设置和检索数据的访问器和mutator方法 public class Product { //fields private String productName; private double cost; /** * Contructor * * @param productName
public class Product {
//fields
private String productName;
private double cost;
/**
* Contructor
*
* @param productName
* @param cost
*/
public Product(String productName, double cost){
this.productName = productName;
this.cost = cost;
}
...
InventoryItem类在产品信息旁边添加数量。这将在Store类中明确说明原因。
我想在构造函数中传递一个新的Product对象作为参数,以便于以后将所有信息添加到ArrayList中。如上所述,我为name、cost和quantity创建了set和get方法
public class InventoryItem {
//fields
private Product productObject;
private int quantity;
/**
* Constructor
*
* @param quantity
*/
public InventoryItem(new Product, int quantity) {
this.quantity = quantity;
}
/**
* Method to set new name for product in inventory
*
* @param newProductName
*/
public void setProductName(String newProductName) {
productObject.setProductName(newProductName);
}
...
我还想知道,一旦我知道如何将对象作为参数传递,我将如何使用Products方法
当尝试用InventotyItems预填充ArrayList时,我开始出错。这是在我的商店课上完成的
import java.util.ArrayList;
public class Store {
//fields
private String storeName;
private String location;
private ArrayList<InventoryItem> itemList;
/**
* Constructor
*/
public Store(String storeName, String location){
this.storeName = storeName;
this.location = location;
itemList = new ArrayList<>();
}
/**
* Method to prepopulate a list of items the store will be selling
*/
private void setItemList(){
itemList.add(new InventoryItem(new Product("Bananas", 1.50)20));
itemList.add(new InventoryItem(new Product("Canned Beans", 2.00)15));
itemList.add(new InventoryItem(new Product("Easy-Mac", 2.50)15));
itemList.add(new InventoryItem(new Product("Oranges", .50)25));
itemList.add(new InventoryItem(new Product("Cereal", 3.00)10):);
itemList.add(new InventoryItem(new Product("Milk", 4.00)10));
}
我想找出如何通过Store类的代码块传递新产品,如上所示。您的语法错误。实例化
InventoryItem
时缺少逗号
import java.util.ArrayList;
public class Store {
//fields
private String storeName;
private String location;
private ArrayList<InventoryItem> itemList;
/**
* Constructor
*/
public Store(String storeName, String location){
this.storeName = storeName;
this.location = location;
itemList = new ArrayList<>();
}
/**
* Method to prepopulate a list of items the store will be selling
*/
private void setItemList(){
itemList.add(new InventoryItem(new Product("Bananas", 1.50)20));
itemList.add(new InventoryItem(new Product("Canned Beans", 2.00)15));
itemList.add(new InventoryItem(new Product("Easy-Mac", 2.50)15));
itemList.add(new InventoryItem(new Product("Oranges", .50)25));
itemList.add(new InventoryItem(new Product("Cereal", 3.00)10):);
itemList.add(new InventoryItem(new Product("Milk", 4.00)10));
}
替换
new InventoryItem(new Product("Bananas", 1.50)20)
与
你的语法错了。实例化
InventoryItem
时缺少逗号
替换
new InventoryItem(new Product("Bananas", 1.50)20)
与
更改Inventory Item构造函数以这样传递数据
public class InventoryItem {
//fields
private Product productObject;
private int quantity;
/**
* Constructor
*
* @param quantity
*/
public InventoryItem(new Product, int quantity) {
this.quantity = quantity;
}
public InventoryItem(String productName, double cost, int quantity) {
this.productObject = new Product(productName,cost);
this.quantity = quantity;
}
/**
* Method to set new name for product in inventory
*
* @param newProductName
*/
public void setProductName(String newProductName) {
productObject.setProductName(newProductName);
}
更改Inventory Item构造函数以这样传递数据
public class InventoryItem {
//fields
private Product productObject;
private int quantity;
/**
* Constructor
*
* @param quantity
*/
public InventoryItem(new Product, int quantity) {
this.quantity = quantity;
}
public InventoryItem(String productName, double cost, int quantity) {
this.productObject = new Product(productName,cost);
this.quantity = quantity;
}
/**
* Method to set new name for product in inventory
*
* @param newProductName
*/
public void setProductName(String newProductName) {
productObject.setProductName(newProductName);
}
更改
InventoryItem
构造函数。构造函数应该获得像(Product)
这样的参数,而不是使用new关键字
public InventoryItem(Product product, int quantity) {
this.quantity = quantity;
}
更改
InventoryItem
构造函数。构造函数应该获得像(Product)
这样的参数,而不是使用new关键字
public InventoryItem(Product product, int quantity) {
this.quantity = quantity;
}
你会遇到什么错误?
新产品(“香蕉”,1.50)20)
你是忘记了逗号还是这只是一个复制粘贴错误?很抱歉,我问题的最后一部分让人困惑。当我说“如上所示”时,我指的是如何在上面的代码块中预填充ArrayList。我想使用该方法创建新的库存对象。您会遇到什么错误?新产品(“香蕉”,1.50)20
您是否忘记了逗号,或者这只是复制粘贴错误?很抱歉,我问题的最后一部分令人困惑。当我说“如上所示”时,我指的是如何在上面的代码块中预填充ArrayList。我想用这个方法来创建新的库存对象。谢谢你的语法帮助。我肯定会错过那个逗号。:)谢谢你的语法帮助。我肯定会错过那个逗号。:)当我像itemList.add(newinventoryItem(newproduct(String,double),int))那样预填充ArrayList时,不确定重载构造函数是否有帮助
@aru如果要填充对象,如itemList.add(newInventoryItem(“香蕉”,1.50,20))
如问题中所述,这将被使用。不确定当我预填充ArrayList(如itemList.add)时,重载构造函数是否会有所帮助(new InventoryItem(new Product(String,double),int))
@aru如果要填充对象,如itemList.add(newInventoryItem(“香蕉”,1.50,20))代码>如问题中所述,这将被使用。