Java:如何在静态方法中创建对象并调用另一个类的方法?

标签 java class object methods static

我花了几个小时完成这个 Java 作业,并且在这个测试类上呆了将近 5 个小时。

在此作业中,我创建了一个 Product 类、一个 Money 类、一个 LineItem 类和一个 Inventory 类。现在我需要创建一个测试类来通过将新的订单项放入库存数组来测试程序。

在测试器类中,我试图创建一个静态方法 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); 
}

当前错误是类型不兼容 - 找到了 LineItem 但需要 Inventory,因此我尝试将 Inventory[] _items; 更改为 LineItem[] _items; >。但错误是变量_items可能未初始化。

抱歉大家,我是一个真正的 Java 菜鸟,我尝试在网上搜索很长时间,但我不太明白大多数结果。我唯一理解的是 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;
   }
}

我还没有对这些方法发表评论,但一旦我理解了它,我就会这样做。

最佳答案

您的数组属于Inventory[]类型 - 但您试图分配LineItem类型的引用。您没有初始化它。更改此:

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

...接下来考虑一下您实际上想要使用 items 变量做什么

关于Java:如何在静态方法中创建对象并调用另一个类的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10236792/

相关文章:

java - 在 Java 中锁定文件 channel 时出现 OverlappingFileLockException

PHP 单例和继承

Java - 将类作为参数传递以进行比较

python - 使用动态 base_class 在 __new__ 子类中设置属性

c# - 检查对象是否属于同一类型

arrays - 如何在 Matlab 中创建指向对象的指针数组?

java - java中私有(private)对象是如何传递的?

java - Imgproc.cvtColor参数含义

java - Intellij toString 多行

java - 如何在jboss中部署资源适配器