我正在做這個Java任務幾個小時,並堅持這個測試人員類爲非常近5個小時。Java:我如何在靜態方法中創建對象並調用另一個類的方法?
在此作業中,我創建了Product類,Money類,LineItem類和Inventory類。現在我需要創建一個測試類,通過將新的lineitems放入庫存數組來測試該程序。
在測試儀類中,我試圖創建一個靜態方法public static void addTestItems(Inventory theInventory)
,它假設要添加4個項目。對於每個項目,我需要創建一個產品對象,後跟一個LineItem對象以包含新創建的產品。接下來,我需要使用庫存類中的方法將項目添加到庫存類中的數組中。
我曾嘗試過至今:
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);
}
的當前錯誤是incompatible types- found LineItem but expected Inventory
,所以我試圖改變Inventory[] _items;
到LineItem[] _items;
。但錯誤是可變的_items可能不會被初始化。
對不起,我是一個真正的Java中的noob,我試過在線搜索了很久,但我不太瞭解大多數結果。我理解的唯一一個是http://forums.devshed.com/java-help-9/bluej-compiler-error-cannot-find-symbol-variable-object-688573.html,但我厭倦了放入我的背景,但失敗了。我也發現了很多結果,但他們有構造函數和實例變量,我的老師特別提到我不需要它們。
不知道,如果專家能指導我一起,讓我知道我的錯誤。謝謝,謝謝。
庫存類:
/**
* 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;
}
}
我還沒有對這些方法進行評論,但仍然會做這樣一旦我理解這一點。
它*未初始化;你聲明一個對數組的引用,但是不要創建一個數組。至於實際的類型,我們不知道你的類型層次結構,所以我們只是猜測。 – 2012-04-19 21:10:40
或實際上我在庫存類中聲明瞭該數組。但不確定是否可以參考。如何在這裏顯示層次結構以便更好地理解? – Panda 2012-04-19 21:12:16