【问题标题】:Java: How do i create objects in a static method and also call for methods from another class?Java:如何在静态方法中创建对象并调用另一个类的方法?
【发布时间】:2012-05-01 11:34:07
【问题描述】:

我做了几个小时的 Java 作业,并在这个测试员课程上坚持了将近 5 个小时。

在这个作业中,我创建了一个 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 菜鸟,我尝试在线搜索很长时间,但我不太了解大多数结果。我唯一了解的是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;
   }
}

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

【问题讨论】:

  • 没有初始化;您声明对数组的引用,但从不创建数组。至于实际的类型,我们不知道您的类型层次结构,所以我们只是猜测。
  • 或者实际上我在库存类中声明了数组。但不确定是否可以裁判。我如何在此处显示层次结构以便更好地理解?

标签: java class object methods static


【解决方案1】:

您的 arrayInventory[] 类型 - 但您正在尝试分配 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 变量做什么

【讨论】:

  • 嗨......如果你不介意你能告诉我_items[1] = new LineItem(product1,5,unitPrice1);我到底在做什么吗?我是否手动将 LineItem 添加到数组中?因为有人告诉我我需要使用 Inventory 类中的方法。如果我说的是对的……我需要重新考虑逻辑。
  • @Panda:您正在创建LineItem 的新实例,并将数组的元素1 设置为对新实例的引用。没有看到它很难知道Inventory 类是如何参与其中的......
  • 嗨...很抱歉,我刚刚添加了库存类。但主要是我只指public void addItem(LineItem item) { _items[_numItems]= item; _numItems++; },如果它太长了,很抱歉
  • 哦。我可以使用库存类中addItem() 方法中的项目吗??
  • 对不起...但我可以知道为什么当我添加 Inventory addItem(items); 时,它是不允许的。请指出我的错误?
【解决方案2】:
LineItem[] _items = new LineItem[4];

那么索引从0开始而不是从1开始,

_items[4] 

将返回 indexoutofbounds 错误

【讨论】:

    【解决方案3】:

    一些事情:

    incompatible types- found LineItem but expected Inventory
    

    是因为您的数组应该包含 Inventory 对象,但您却将 LineItems 分配给它

    variable _items may not be initialise
    

    意味着你有你的 _items 对象,但你还没有将它初始化为任何东西。你想做的事

    LineItem[] _items = new LineItem[4];
    

    PS:如果您想要动态大小的数组,不知道您可能会加载多少行项目,等等等等使用向量或集合或类似这些行的东西。

    还有,

    _items[1] = new LineItem(product1,5,unitPrice1);
    _items[2] = new LineItem(product2,8,unitPrice2);
    _items[3] = new LineItem(product3,200,unitPrice3);
    _items[4] = new LineItem(product4,9,unitPrice4); 
    

    在 Java 中,数组元素从索引 0 而不是 1 开始

    _items
    

    是一个古怪的变量名,会让你的队友在你的咖啡里打喷嚏

    【讨论】:

    • 哦,是的,我忘记了从 0 开始的索引...谢谢提醒。
    猜你喜欢
    • 1970-01-01
    • 2021-09-25
    • 1970-01-01
    • 1970-01-01
    • 2018-04-20
    • 1970-01-01
    • 2021-10-18
    • 1970-01-01
    • 2011-05-20
    相关资源
    最近更新 更多