Interface 静态上下文错误中使用的非静态方法

Interface 静态上下文错误中使用的非静态方法,interface,arraylist,compiler-errors,static-methods,Interface,Arraylist,Compiler Errors,Static Methods,关于在静态上下文中使用的非静态方法的这段代码,我一直遇到两个错误。此代码使用鸟、猫和狗的不同对象的ArrayList,并使用名为Pet的接口将它们放入名为petList的ArrayList中。 我在第四行和第六行都犯了同样的错误 public static void Feed(ArrayList petList){ Scanner input = new Scanner(System.in); String petName = input.next();

关于在静态上下文中使用的非静态方法的这段代码,我一直遇到两个错误。此代码使用鸟、猫和狗的不同对象的ArrayList,并使用名为Pet的接口将它们放入名为petList的ArrayList中。
我在第四行和第六行都犯了同样的错误

    public static void Feed(ArrayList petList){
        Scanner input = new Scanner(System.in);
        String petName = input.next();
        contains(petName, petList);

        if(ifThere == true){
            String feed = Pet.feed();
            System.out.println(petName + feed);
        }
        else{
            System.out.println("Unknown pet");
        }
    }


  public boolean contains (String petName, ArrayList petList){

    boolean ifThere = false;
    int sizeList = petList.size() -1;
    for(int i=0; sizeList > i; i++){
      Pet booleanPet = petList.get(i);
      String booleanName = booleanPet.getName();
      if (booleanName.equals(petName)){
        ifThere = true;
      }
}
return ifThere;

}

简而言之:您不能从静态方法调用非静态方法。

解决方案:1)将您的“contains”方法设置为静态,它将解决问题

或 2) (假设类的名称为Pet,然后创建Pet类的实例并调用contains方法: 您的第4行可以替换为以下代码(C风格代码):

--额外详情: 静态方法是一种从不特定于任何对象的方法。例如,添加2个数字。您 不需要实例化任何类来调用Math.Add()方法,因为Add是静态方法

你也可以说静态是一种方法,它不是一种你肯定知道的虚拟意义
正在调用哪个方法。

这表明contains和feed不是静态方法。如果没有更多的代码,我们将无法进一步提供帮助。
Pet somePet = new Pet ();
somePet.contains(petName, petList);