Java:如何在静态方法中创建对象,并调用另一个类的方法? [英] Java: How do i create objects in a static method and also call for methods from another class?

查看:342
本文介绍了Java:如何在静态方法中创建对象,并调用另一个类的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在这个赋值中,我创建了一个Product类,一个Money类,LineItem类和Inventory类。现在我需要创建一个测试类,通过将新的lineitems放入库存数组来测试程序。



在测试类,我试图创建一个静态方法 public static void addTestItems(Inventory theInventory)其中假设要添加4个项目。对于每个项目,我需要创建一个产品对象,后面跟一个LineItem对象,以包含新创建的产品。接下来我需要使用库存类中的一个方法将这些项目添加到库存类中的数组中。



到目前为止,我还试过了:

  private static void addTestItems(Inventory theInventory)
{
Inventory [] _items;
产品product1 =新产品(Book,Objects first with Java,优秀的Java入门教程);
产品product2 =新产品(CD,月亮的黑暗面,全时间经典粉红色Floyd专辑);
产品product3 =新产品(DVD,变形金刚,机器人伪装);
产品product4 =新产品(笔记本电脑,联想T42,一个好的但可负担得起的笔记本电脑);
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);
}

当前错误是所以我试图将库存[] _items; 更改为 LineItem [] _items; 。但错误是可变的_items可能不是初始化。



对不起我我是一个真正的noob在Java,我试图在线搜索年龄,但我不太明白大多数结果。我唯一理解的是 http://forums.devshed.com/java-help-9/bluej-compiler-error-cannot-find-symbol-variable-object-688573.html 但我厌倦了我的上下文,但失败。我也发现很多结果,但他们有构造函数和实例变量,我的老师特别提到,我不需要他们。



不知道专家可以指导我一样让我知道我的错误。非常感谢。



库存类别:

  / ** 
*在Inventory类中,它只是创建一个产品的列表/数组,它允许来自linitem的信息与索引一起放置。
*例如,对于第一个产品,我们可以使用库存类别将其输入到索引1中,并将他的下一个产品输入到索引2中,依此类推。
*它是用来创建一个数组和输入lineitem信息到它。
*
* @author(您的姓名)
* @version(版本号或日期)
* /
public class Inventory
{
//实例变量 - 用下面的例子替换你自己的
private LineItem [] _items;
private int _numItems;


/ **
*类库的对象的构造方法
* /
public Inventory()
{
/ / initialise实例变量
_items = new LineItem [1000];
_numItems = 0;
}

/ **
*一个方法的例子 - 用你自己的
*
* @param ya替换这个注释a方法
* @返回x和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 $ getTotalValue()
{
int i = 0;
钱总=新钱(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 []
- 但您尝试分配 LineItem 类型的引用。您不初始化它。更改为:

 库存[] _items; 

  LineItem [] _items = new LineItem [5]; 

这一切都应该很好 - 虽然你不使用索引0



使用数组的另一种方法是使用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));

...下一步想想你实际想要 变量。


I am doing this Java assignment for hours and stuck with this tester class for very almost 5 hours.

In this assignment, I have created a Product class, a Money class, a LineItem class and an Inventory class. Now i need to create a test class to test the program by putting new lineitems into the inventory array.

In the tester class, I am trying to create a static method public static void addTestItems(Inventory theInventory) which suppose to add 4 items. For each item I will need to create a product object followed by a LineItem object to contain the newly created product. next i need to use a method from the inventory class to add the items into the array in the inventory class.

What i have tried too so far:

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); 
}

The current error is incompatible types- found LineItem but expected Inventory so i tried changing Inventory[] _items; to LineItem[] _items;. But the error was variable _items may not be initialise.

Sorry guys I am a real noob in Java, I tried searching on-line for ages but I do not quite understand most results. The only one i understand was http://forums.devshed.com/java-help-9/bluej-compiler-error-cannot-find-symbol-variable-object-688573.html but i tired putting into my context but failed. I also found lot of results but they had constructors and instance variables in them which my teacher specifically mentioned that I will not need them.

Wonder if expert could guide me along like let me know my mistakes. Thanks thanks.

The inventory class:

/**
* 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;
   }
}

I have yet to comment on the methods yet but will do so once i understand it.

解决方案

Your array is of type Inventory[] - but you're trying to assign references of type LineItem. You're also not initializing it. Change this:

Inventory[] _items;

to this:

LineItem[] _items = new LineItem[5];

And all should be well - although you're not using index 0 (which is why you need it to be size 5) and you're not doing anything with the array afterwards either...

Another alternative to using an array is to use a List:

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));

... next think about what you actually want to do with the items variable.

这篇关于Java:如何在静态方法中创建对象,并调用另一个类的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆