Java:如何在静态方法中创建对象,并从另一个类中调用方法?
我花了几个小时来完成这个Java任务,并且花了将近5个小时来学习这个tester类 在本作业中,我创建了一个产品类、一个货币类、一个行项目类和一个库存类。现在我需要创建一个测试类,通过将新的lineitems放入inventory数组来测试程序 在tester类中,我试图创建一个静态方法Java:如何在静态方法中创建对象,并从另一个类中调用方法?,java,class,object,methods,static,Java,Class,Object,Methods,Static,我花了几个小时来完成这个Java任务,并且花了将近5个小时来学习这个tester类 在本作业中,我创建了一个产品类、一个货币类、一个行项目类和一个库存类。现在我需要创建一个测试类,通过将新的lineitems放入inventory数组来测试程序 在tester类中,我试图创建一个静态方法publicstaticvoidaddtestitems(Inventory theInventory),该方法假设添加4项。对于每个项目,我需要创建一个product对象,后跟一个LineItem对象,以包含新
publicstaticvoidaddtestitems(Inventory theInventory)
,该方法假设添加4项。对于每个项目,我需要创建一个product对象,后跟一个LineItem对象,以包含新创建的产品。接下来,我需要使用inventory类中的一个方法将项目添加到inventory类中的数组中
到目前为止我已经尝试过的:
private static void addTestItems(Inventory theInventory)
{
Inventory[] _items;
Product product1 = new Product("Book","Objects first with Java"," An excellent introductory Java textbook");
Product product2 = new Product("CD","The dark side of the moon","The all-time classic Pink Floyd album");
Product product3 = new Product("DVD", "Transformers","Robots in disguise");
Product product4 = new Product("Laptop","Lenovo T42","A good yet affordabble laptop");
Money unitPrice1 = new Money(29,99);
Money unitPrice2 = new Money(4,99);
Money unitPrice3 = new Money(9,99);
Money unitPrice4 = new Money(450,0);
_items[0] = new LineItem(product1,5,unitPrice1);
_items[1] = new LineItem(product2,8,unitPrice2);
_items[2] = new LineItem(product3,200,unitPrice3);
_items[3] = new LineItem(product4,9,unitPrice4);
}
当前错误是不兼容类型-找到了行项目,但需要库存
,因此我尝试更改库存[]\u项目代码>至行项目[]\u项目代码>。但错误是可变的_项可能无法初始化
对不起,伙计们,我在Java是一个真正的noob,我尝试在网上搜索了很久,但我不太了解大多数结果。我唯一能理解的是,我对自己的处境感到厌倦,但失败了。我也发现了很多结果,但其中有构造函数和实例变量,我的老师特别提到我不需要它们
不知道专家是否能像让我知道我的错误那样指导我。谢谢
库存类别:
/**
* In the Inventory class, it is merely to create a list / array of product which allows the information from the linitem to be put with an index.
* For example, for the first product, we can use the inventory class to input it into the index 1. and he next product into index 2 and so on.
* It is suse to create an array and inputing the lineitem information into it.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Inventory
{
// instance variables - replace the example below with your own
private LineItem[] _items;
private int _numItems;
/**
* Constructor for objects of class Inventory
*/
public Inventory()
{
// initialise instance variables
_items = new LineItem[1000];
_numItems = 0;
}
/**
* An example of a method - replace this comment with your own
*
* @param y a sample parameter for a method
* @return the sum of x and y
*/
public void addItem(LineItem item)
{
_items[_numItems]= item;
_numItems++;
}
public String toString()
{
String result="";
int i=0;
while (i < _numItems)
{
result = result + _items[i] + "/n";
i++;
}
return result;
}
public void print()
{
String myResult=this.toString();
System.out.println(myResult);
}
public Money getTotalValue()
{
int i=0;
Money total= new Money(0);
while (i<_items.length)
{
total = total.add(Money.NO_MONEY);
i++;
}
return total;
}
public LineItem getItem(String productName)
{
int i = 0;
LineItem itemDetails = null;
while (i<_items.length)
{
if (_items[i].equals(productName))
{
itemDetails= _items[i];
}
else
{
//do nothing
}
i++;
}
return itemDetails;
}
}
/**
*在Inventory类中,只需创建一个产品列表/数组,该列表/数组允许将linitem中的信息与索引一起放置。
*例如,对于第一个产品,我们可以使用inventory类将其输入索引1。他将下一个产品放入索引2中,以此类推。
*可以创建一个数组并将行项目信息输入其中。
*
*@author(你的名字)
*@version(版本号或日期)
*/
公共类目录
{
//实例变量-将下面的示例替换为您自己的
专用行项目[]_项;
私营企业;
/**
*类资源清册对象的构造函数
*/
公共库存()
{
//初始化实例变量
_项目=新的行项目[1000];
_numItems=0;
}
/**
*方法示例-将此注释替换为您自己的注释
*
*@param y方法的示例参数
*@返回x和y的总和
*/
公共无效附加项(行项目)
{
_项目[_numItems]=项目;
_numItems++;
}
公共字符串toString()
{
字符串结果=”;
int i=0;
而(我<\u numItems)
{
结果=结果+_项[i]+“/n”;
i++;
}
返回结果;
}
公开作废印刷品()
{
字符串myResult=this.toString();
System.out.println(myResult);
}
公共资金获得总价值()
{
int i=0;
货币总额=新货币(0);
虽然(i您的数组类型为库存[]
,但您正在尝试分配行项目类型的引用。您也没有初始化它。更改此设置:
Inventory[] _items;
为此:
LineItem[] _items = new LineItem[5];
所有这些都应该是好的-尽管你没有使用索引0(这就是为什么你需要它的大小为5),而且你也没有在以后对数组做任何事情
使用数组的另一种替代方法是使用列表:
List<LineItem> items = new ArrayList<LineItem>();
items.add(new LineItem(product1, 5, unitPrice1));
items.add(new LineItem(product2, 8, unitPrice2));
items.add(new LineItem(product3, 200, unitPrice3));
items.add(new LineItem(product4, 9, unitPrice4));
List items=new ArrayList();
添加(新的行项目(产品1、5、单价1));
添加(新的行项目(产品2、8、单价2));
添加(新的行项目(Product3200,单价3));
添加(新的行项目(产品4、9、单价4));
…接下来考虑一下您实际希望如何处理项
变量
LineItem[] _items = new LineItem[4];
然后索引从0开始,而不是从1开始
_items[4]
将返回indexoutofbounds错误一些内容:
incompatible types- found LineItem but expected Inventory
这是因为您的数组应该包含库存对象,但您将为其指定行项目
variable _items may not be initialise
意味着您有您的_items对象,但尚未将其初始化为任何对象。您要执行以下操作
LineItem[] _items = new LineItem[4];
PS:如果你想要动态大小的数组,你不知道有多少行项目你可能会加载,等等,使用一个向量或集合或沿着这些行的东西
而且
在Java中,数组元素以索引0而不是1开始
_items
是一个不可靠的变量名,会让你的队友在你的咖啡中打喷嚏它没有初始化;你声明了对数组的引用,但从未创建数组。至于实际的类型,我们不知道你的类型层次结构,所以我们只能猜测。或者实际上我在inventory类中声明了数组。但不确定它是否可以被引用。H我如何在这里显示层次结构以更好地理解?嗨…如果你不介意的话,你能告诉我通过\u items[1]=new LineItem(product1,5,unitPrice1)我到底在做什么吗;
?我是否手动将LineItem添加到数组中?因为有人告诉我需要使用Inventory类中的方法。如果我说的是对的,我需要重新考虑逻辑。@Panda:您正在创建LineItem
的新实例,并将数组的元素1设置为对新实例的引用。很难知道wInventory
类是如何在没有看到它的情况下参与进来的…嗨…很抱歉我刚刚添加了Inventory类。但主要是指public void addItem(LineItem item){u items[\u numItems]=item;\u numItems++;}
如果太长,那么很抱歉。我可以使用addItem()中的项目吗
库存类中的方法??抱歉…但是我可以知道为什么在添加库存添加项(项目);
时,它是不允许的。请指出我的错误?哦,是的,我忘记了从0开始的索引…谢谢提醒。
_items