2016-04-27 50 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; 
     } 

    } 
} 

,這裏是產品類:

/** 
* 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_'''嗎? – rgettman

+2

歡迎來到Stack Overflow。請始終嘗試將問題減少到[mcve] - 這裏包含了超過250行的代碼,用於可能在大約15次顯示的內容。 –

+0

哦,我的上帝..我一直在試圖解決這個問題像現在三天。非常感謝! – Blaza

回答

0

您在致電Product.getID()。 Product是類名稱,您可以以這種方式調用的唯一方法是靜態類方法。 在你的情況下,getID()不是一個靜態方法,因此應該由該類的一個實例調用。產品不是Product的實例,參照對象本身,p應該是小寫的。

0

替換:

if(Product.getID() == item) 

有:

if(product.getID() == item) 

Product(大寫P)是類名,而product是實例名。

0

getID方法是一種實例方法。這意味着你將不得不爲它創建一個對象,以便使用它並訪問這個類的數據字段,以便它可以返回創建的OBJECT的ID。

您實際上是通過調用類名來訪問方法,就像調用靜態方法一樣。但它不會通過class.methodName(); 調用它直接嘗試直接調用它。

相關問題