1

クラスのオブジェクトのリストを作成するメソッドがあります

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()メソッドが type に対して未定義であるというエラーが表示されList<Product>ます。他のメソッドについても同様です。このエラーで私を助けてください。

私の発言は正しいですか??

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

4 に答える 4

4

productの参照ですList

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

そのメソッドを持っていない

于 2013-07-31T04:35:13.863 に答える
1

私の発言は正しいですか??

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

いいえ、これは正しくありません。product は class のインスタンスではProductなく、 のインスタンスですList。List には と呼ばれるメソッドがありませんgetId

リストから要素を取得し、それを使用して別のインスタンスを作成する場合は、次のようにすることができます。

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

ただし、リストに要素があることを確認してください。そうしないと、例外が発生する可能性があります。product.add(製品);

于 2013-07-31T04:37:07.090 に答える