2016-04-27 21 views
-1

この件に関する他の回答を見ています。ここで私が持っているクラスです。静的なコンテキストから非静的メソッドを参照することはできませんが、静的なものは何もありません。

import java.util.ArrayList; 

/** 
* Manage the stock in a business. 
* The stock is described by zero or more Products. 
* 
* @author 
* @version 
*/ 
public class StockManager 
{ 
// A list of the products. 
private ArrayList<Product> stock; 

/** 
* Initialise the stock manager. 
*/ 
public StockManager() 
{ 
    stock = new ArrayList<Product>(); 
} 

/** 
* Add a product to the list. 
* @param item The item to be added. 
*/ 

public void addProduct(Product item) 
{ 

    for(Product product: stock) 
    { 
     if(Product.getID() == item) 
     { 
      System.out.println("Please add an item with a different id"); 
     } 

     else 
     { 
     stock.add(item); 

     } 
    } 
} 

/** 
* Receive a delivery of a particular product. 
* Increase the quantity of the product by the given amount. 
* @param id The ID of the product. 
* @param amount The amount to increase the quantity by. 
*/ 

public void delivery(int id, int amount) 
{ 
    for(Product product : stock) 
    { 
     if(findProduct(id) != null) 
     { 
      increaseQuantity(amount); 

     } 

    } 
} 

/** 
* Try to find a product in the stock with the given id. 
* @return The identified product, or null if there is none 
*   with a matching ID. 
*/ 

public Product findProduct(int id) 
{ 
    for(Product product : stock) 
    { 
     if(id == getID()) 
     { 
     return getName(); 
     } 

     else 
     { 
      return null; 
     } 



    } 
} 

/** 
* Locate a product with the given ID, and return how 
* many of this item are in stock. If the ID does not 
* match any product, return zero. 
* @param id The ID of the product. 
* @return The quantity of the given product in stock. 
*/ 
public int numberInStock(int id) 
{ 
    for(Product product : stock) 
    { 
     if(id == getID()) 
     { 
      getQuantity(); 
     } 

     else 
     { 
      return 0; 
     } 
    } 


} 

/** 
* Print details of all the products. 
*/ 
public void printProductDetails() 
{ 
    for(Product product: stock) 
    { 
     toString(); 
    } 
} 


/** 
* This method prints the deatils 
* of products under a certain quantity 
* @param the amount you want to check under 
*/ 
public void lowStockCheck(int amount) 
{ 
    for(Product product : stock) 
    { 
     if(getQuantity() < amount) 
     { 
      printProductDetails(); 
     } 
    } 

} 

/** 
* Find a product via it's name 
* rather than ID 
* @param Name of product 
*/ 
public Product findProductName(String name) 
{ 
    for(Product product : stock) 
    { 
     if(name.equals(getName())) 
     { 
      return getName(); 
     } 
     else 
     { 
      return null; 
     } 

    } 
} 

そしてここでは、Productクラスである:

/** 
* Model some details of a product sold by a company. 
* 
* @author David J. Barnes and Michael Kölling. 
* @version 2011.07.31 
*/ 
public class Product 
{ 
    // An identifying number for this product. 
    private int id; 
    // The name of this product. 
    private String name; 
    // The quantity of this product in stock. 
    private int quantity; 

/** 
* Constructor for objects of class Product. 
* The initial stock quantity is zero. 
* @param id The product's identifying number. 
* @param name The product's name. 
*/ 
public Product(int id, String name) 
{ 
    this.id = id; 
    this.name = name; 
    quantity = 0; 
} 

/** 
* @return The product's id. 
*/ 
public int getID() 
{ 
    return id; 
} 

/** 
* @return The product's name. 
*/ 
public String getName() 
{ 
    return name; 
} 

/** 
* @return The quantity in stock. 
*/ 
public int getQuantity() 
{ 
    return quantity; 
} 

/** 
* @return The id, name and quantity in stock. 
*/ 
public String toString() 
{ 
    return id + ": " + 
      name + 
      " stock level: " + quantity; 
} 

/** 
* Restock with the given amount of this product. 
* The current quantity is incremented by the given amount. 
* @param amount The number of new items added to the stock. 
*    This must be greater than zero. 
*/ 
public void increaseQuantity(int amount) 
{ 
    if(amount > 0) { 
     quantity += amount; 
    } 
    else { 
     System.out.println("Attempt to restock " + 
          name + 
          " with a non-positive amount: " + 
          amount); 
    } 
} 

/** 
* Sell one of these products. 
* An error is reported if there appears to be no stock. 
*/ 
public void sellOne() 
{ 
    if(quantity > 0) { 
     quantity--; 
    } 
    else { 
     System.out.println(
      "Attempt to sell an out of stock item: " + name); 
    } 
} 

私が手にエラーがのStockManagerクラスのaddProduct機能です。この時点で時間内に我々は彼らを必要としないように、コードのこの作品は、具体的

/** 
* Add a product to the list. 
* @param item The item to be added. 
*/ 

public void addProduct(Product item) 
{ 

    for(Product product: stock) 
    { 
     if(Product.getID() == item) 
     { 
      System.out.println("Please add an item with a different id"); 
     } 

     else 
     { 
     stock.add(item); 

     } 
    } 
} 

エラーが言う、これらの方法のどちらが静的であり、「非静的メソッドのgetId()は、静的なコンテキストから参照することはできません」 。私はここで何が欠けていますか?

+5

'product.getID()'は、_lowercase_ 'p'を使用しましたか? – rgettman

+2

ようこそスタックオーバーフロー。問題を[mcve]に減らそうとしてください。約250行以上のコードが含まれています。約15で表示されていた可能性があります。 –

+0

ああ、私はこの問題を解決しようとしてきました。今の3日間のように。どうもありがとうございます! – Blaza

答えて

0

あなたはProduct.getID()を呼び出しています。 Productはクラス名であり、このように呼び出すことができる唯一のメソッドは静的クラスメソッドです。 getID()は静的メソッドではないため、クラスのインスタンスによって呼び出される必要があります。 ProductはProductのインスタンスではないため、pはオブジェクト自体を参照するために小文字にする必要があります。

0

は置換:

if(Product.getID() == item) 

で:productインスタンス名である

if(product.getID() == item) 

Product(大文字のPを有する)はクラス名です。

0

getIDメソッドはインスタンスメソッドです。つまり、オブジェクトを作成してこのクラスのデータフィールドにアクセスして、作成されたOBJECTのIDを返すことができるようにする必要があります。

実際に静的メソッドを呼び出すように、クラス名を呼び出すことでメソッドにアクセスしています。しかしそれはそれによって呼び出されませんclass.methodName(); それを直接呼び出してみてください。

関連する問題