Java 无法从静态上下文引用非静态方法,但没有任何内容是静态的

Java 无法从静态上下文引用非静态方法,但没有任何内容是静态的,java,Java,我看过关于这个问题的其他答案,没有找到任何帮助。以下是我的课程: 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 ArrayLi

我看过关于这个问题的其他答案,没有找到任何帮助。以下是我的课程:

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

    }
}
我得到的错误在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()”,这两个方法都不是静态的,因为此时我们不需要它们。这里缺少什么?

您正在调用Product.getID()。Product是类名,您可以以这种方式调用的唯一方法是静态类方法。 在您的例子中,getID()不是静态方法,因此应该由类的实例调用。Product不是Product的实例,相对于对象本身,p应该是小写的。

替换:

if(Product.getID() == item)
与:


Product
(大写p)是类名,
Product
是实例名。

getID方法是实例方法。这意味着您必须为它创建一个对象,以便使用它并访问此类的数据字段,以便它可以返回所创建对象的ID

实际上,您是通过调用类名来访问该方法的,就像调用静态方法一样。但是它不会通过
class.methodName()调用它
试着直接给它打电话


您的意思是
product.getID()
,带有小写的
p
?欢迎使用堆栈溢出。请尽量把你的问题简化成一个问题——你已经在这里包含了超过250行的代码,这些代码本可以在大约15行中显示出来。哦,我的天。。我已经试着解决这个问题三天了。非常感谢你!
if(Product.getID() == item)
if(product.getID() == item)