错误:对于类型List <product> </product>,方法getId()未定义

时间:2013-07-31 04:34:37

标签: java

我有一个方法来创建类

的对象列表
public List<Product> initProducts(){
    List<Product> product = new ArrayList<Product>();

        Product prod = new Product(product.getId(),product.getItemName(),product.getPrice(),product.getCount());
        product.add(prod);

    return product;
}

我的产品类是:

public class Product {

int ItemCode;
String ItemName;
double UnitPrice;
int Count;

/**
 * Initialise the fields of the item.
 * @param Name The name of this member of product.
 * @param id The number of this member of product.
 * @param Price The price of this member of product.
 */

public Product(int id, String Name, double Price, int c)
{
    ItemCode=id;
    ItemName=Name;
    UnitPrice=Price;
    Count = c;
}

public int getId()
{
   return this.ItemCode;
}

public String getItemName()
{
   return this.ItemName;
}

public double getPrice()
{
   return this.UnitPrice;
}

public int getCount()
{
   return this.Count;
}



/**
 * Print details about this members of product class to the text terminal.
 */
public void print()
{
    System.out.println("ID: " + ItemCode);
    System.out.println("Name: " + ItemName);
    System.out.println("Staff Number: " +UnitPrice);
    System.out.println("Office: " + Count);

}
}

我收到的错误是类型getId()的方法List<Product>未定义,其他方法也是如此。请帮我解决这个错误。

我的陈述是否正确?

Product prod = new Product(product.getId(),product.getItemName(), product.getPrice(),  
product.getCount());
product.add(prod);

4 个答案:

答案 0 :(得分:4)

productList

的参考
List<Product> product = new ArrayList<Product>();

没有那种方法

答案 1 :(得分:2)

product是对List对象的引用。

List/ArrayList没有名为getId()的方法。

您已为getId()类编写了Prodct方法,因此您可以使用ref Product类对象调用此方法。

如果您想获取任何产品对象表单列表,请使用get(int index)的{​​{1}}方法。

例如

ArrayList

答案 2 :(得分:2)

我相信,你面临这个问题的原因更多的是因为没有遵守代码约定,其他任何一个。

每当您创建任何对象的集合时,约定是使用复数作为集合的引用名称。和Object本身的单数引用名称。 您可以找到更多详细信息here.

下面是重写代码,其中遵循了代码约定:

创建类Product对象列表的方法:

public List<Product> initProducts(){
    List<Product> products = new ArrayList<Product>();
    Product product = new Product(products.getId(), products.getItemName(), products.getPrice(), products.getCount());
    products.add(prod);    
}

产品类别:

class Product {

int itemCode;
String itemName;
double unitPrice;
int count;

public Product(int itemCode, String itemName, double unitPrice, int count)
{
    this.itemCode = itemCode;
    this.itemName = itemName;
    this.unitPrice = unitPrice;
    this.count = count;
}

public int getId()
{
   return this.itemCode;
}

public String getItemName()
{
   return this.itemName;
}

public double getPrice()
{
   return this.unitPrice;
}

public int getCount()
{
   return this.count;
}
}

现在,很容易看出,产品Object(属于List类型)将没有任何方法名称getId()或getCount()。事实上,这些是List中包含的Object的方法。

以下约定将帮助您避免未来的麻烦。

答案 3 :(得分:1)

我的陈述是否正确?

Product prod = new Product(product.getId(),product.getItemName(), product.getPrice(),  
product.getCount());
product.add(prod);

不,这是不正确的。 product不是类Product的实例,而是List的实例。 List没有任何名为getId的方法。

如果您想从列表中检索元素并使用它来创建另一个实例,您可以执行以下操作:

Product exisProd = product.get(0);
Product prod = new Product(exisProd .getId(),exisProd .getItemName(), exisProd .getPrice(),  
    exisProd .getCount());

但请确保列表中包含元素,否则您可能会遇到异常。     product.add(PROD);