简体   繁体   English

错误:未为类型List定义方法getId() <Product>

[英]Error: the method getId() is undefined for the type List<Product>

I have a method to create a list of objects of class 我有一种方法来创建类的对象列表

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

My Product class is: 我的产品类别是:

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

}
}

I am getting an error that the method getId() is undefined for the type List<Product> , Similarly for other methods. 我收到一个错误,指出类型List<Product>的方法getId()未定义,其他方法也是如此。 Please help me out with this error. 请帮助我解决此错误。

Is my statement correct?? 我的说法正确吗?

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

product is a reference of List productList的参考

List<Product> product = new ArrayList<Product>();

which doesn't have that method 没有那个方法

product is reference to List object. product是对List对象的引用。

and List/ArrayList has no methosd named getId() . 并且List/ArrayList没有名为getId()

You have written getId() method for Prodct class , so you can call this method using ref to Product class object. 您已经为Prodct类编写了getId()方法,因此可以使用对Product类对象的引用来调用此方法。

If you want to get any product object form list use get(int index) method of ArrayList . 如果要获取任何产品对象表单列表,请使用ArrayList get(int index)方法。

eg. 例如。

Product prod = product.get(0);
String id= prod.getId();

I believe, the reason you are facing this issue is more due not following the code conventions, that any other. 我相信,您面临此问题的原因更多是因为未遵循任何其他代码约定。

Whenever you make a collection of any objects, the convention is to use plurals for reference names of the collection. 每当您对任何对象进行集合时,约定都是使用复数作为集合的引用名称。 And singular reference name of the Object itself. 和Object本身的引用名称。 You can find more details here. 您可以在此处找到更多详细信息

Below is the re-written code with the code conventions being followed: 下面是重写的代码,遵循的代码约定:

Method to create a list of objects of class Product: 创建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);    
}

Product Class: 产品类别:

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

Now, it is easy to see, that the products Object (which is of the type List) will not have any methods name getId() or getCount(). 现在,很容易看出,产品对象(类型为List)将没有任何名为getId()或getCount()的方法。 Infact , these are methods of the Object contained in the List. 实际上,这些是List中包含的Object的方法。

Following conventions will help you avoid, such hassles in futures. 遵循约定将有助于您避免此类期货交易的麻烦。

Is my statement correct?? 我的说法正确吗?

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

NO this is incorrect. 不,这是不正确的。 product is not an instance of class Product ,rather it is an instance of List . product不是Product类的实例,而是List的实例。 List does not have any method called getId . List没有任何名为getId

If you want to retrieve the elements from the list and use it to create another instance of you can do something like: 如果要从列表中检索元素并使用它创建另一个实例,可以执行以下操作:

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

But make sure that you have elements in the list, otherwise u may run into exception. 但是请确保您在列表中有元素,否则您可能会遇到异常。 product.add(prod); product.add(prod);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM