Java 为什么我的代码在所有三个产品对象中存储相同的数据?
在过去的一天里,我一直在努力解决这个问题,但我做的每一个调整似乎都会把事情搞得更糟。我应该补充一点,我对java和一般编程都比较陌生 我的问题是,在我的程序中,我有三个产品对象之一的用户输入数据,但是当我读取所有三个对象的数据时,相同的数据存储在所有对象中。该程序编译和运行良好,所以我完全不知道我做错了什么 这是接口类中的方法:Java 为什么我的代码在所有三个产品对象中存储相同的数据?,java,class,object,methods,getter,Java,Class,Object,Methods,Getter,在过去的一天里,我一直在努力解决这个问题,但我做的每一个调整似乎都会把事情搞得更糟。我应该补充一点,我对java和一般编程都比较陌生 我的问题是,在我的程序中,我有三个产品对象之一的用户输入数据,但是当我读取所有三个对象的数据时,相同的数据存储在所有对象中。该程序编译和运行良好,所以我完全不知道我做错了什么 这是接口类中的方法: private void readInput() // the only method in the program that accepts product data
private void readInput() // the only method in the program that accepts product data from the user
{
Store matesStore = new Store();
String name;
int demandRate, productChoice;
double setupCost, unitCost, inventoryCost, sellingPrice;
Scanner console = new Scanner(System.in);
System.out.println("Please choose a product: (1), (2), (3)"); //this is where the user chooses which product they wish to enter data for.
productChoice = console.nextInt();
System.out.println("Please enter the product's name: ");
name = console.next();
Store.check(name);
//checks to make sure that the user doesn't enter the same product name more than once
System.out.println("Please enter the product's demand rate: ");
demandRate = console.nextInt();
System.out.println("Please enter the product's setup cost: ");
setupCost = console.nextDouble();
System.out.println("Please enter the product's unit cost: ");
unitCost = console.nextDouble();
System.out.println("Please enter the product's inventory cost: ");
inventoryCost = console.nextDouble();
System.out.println("Please enter the product's selling price: ");
sellingPrice = console.nextDouble();
matesStore.addData(productChoice, name, demandRate, setupCost, unitCost, inventoryCost, sellingPrice);
// uses the addData() method in the Store class to set the data in the created Store object matesStore
//continueOption(); //is meant to ask the user whether they are ready to continue but I still have some problems with it
showInterface();
chooseOption();
}
下面是Store类中的方法:
public static void addData(int option, String newName, int newDemand, double newSetup, double newUnit, double newInventory, double newPrice)
//is meant to set the product data for a particular product
{
if (option==1) setData(product1, newName, newDemand, newSetup, newUnit, newInventory, newPrice);
else if (option==2) setData(product2, newName, newDemand, newSetup, newUnit, newInventory, newPrice);
else /*(option==3)*/ setData(product3, newName, newDemand, newSetup, newUnit, newInventory, newPrice);
}
private static void setData(Product product, String name, int demandRate, double setupCost, double unitCost, double inventoryCost, double sellingPrice)
//uses the Product class' mutator methods to set the data
{
Product.setName(name);
Product.setDemand(demandRate);
Product.setSetup(setupCost);
Product.setUnit(unitCost);
Product.setInventory(inventoryCost);
Product.setPrice(sellingPrice);
}
如果您有任何其他需要帮助的信息,我很乐意提供。任何帮助都将不胜感激
干杯
“为什么我的代码在所有三个产品对象中存储相同的数据?”
看起来所有的
产品
字段都是静态的
,因为您是以静态方式调用set方法的。这将导致所有Product
对象中的值相同。对进行一些研究看起来所有的产品
字段都是静态的
,因为您是以静态方式调用set方法的。这将导致所有产品中的值相同。对静态和实例之间的差异做一些研究谢谢,我现在就来研究:)非常感谢,我能用你的建议解决我的问题。我能给你介绍一下吗?:)我只是把我的评论复制成一个答案。如果愿意,您可以接受:-)