不能从同一 class 中的静态上下文引用非静态方法

Non-static methods cannot be referenced from a static context from within the same class

我已经毫不费力地完成了学生注册界面的大部分代码,但是我遇到了这个错误。

non-static method cannot be referenced from a static context.

现在,我知道这种问题很常见,但它仍然无法点击,而且 none 我发现的问题似乎与我自己的代码足够相似,因此我可以自己找到解决方案,因为此错误与同一 class.

的另一个方法中的方法有关

这是完整的代码,主要区域用注释标记:

import java.util.Scanner;

public class RegistryInterface 
{
   private Registry theRegistry = null;

   public RegistryInterface(Registry theRegistry)
   {

   }

   public void doMenu()
   {
       boolean menuClose = false;

       while (menuClose != true)
       {
           Scanner in = new Scanner(System.in);
           System.out.println("Registry Main Menu");
           System.out.println("******************");
           System.out.println("");
           System.out.println("1) Add a Student");
           System.out.println("2) Delete a Student");
           System.out.println("3) Print Registry");
           System.out.println("4) Quit");
           System.out.println("Select option [1, 2, 3, 4] :> ");
           int option = in.nextInt();

           if (option == 1)
           {
               boolean anotherStudent = false;
               while (anotherStudent != true)
               System.out.println("");
               System.out.println("Add New Student");
               System.out.println("***************");
               System.out.println("");
               System.out.println("Enter forename      :> ");
               String aForeName = in.nextLine();
               System.out.println("Enter surname       :> ");
               String aSurName = in.nextLine();
               System.out.println("Enter student ID    :> ");
               String aID = in.nextLine();
               System.out.println("Enter degree scheme :> ");
               String aDegreeScheme = in.nextLine();
               RegistryInterface.doAddStudent(aForeName, aSurName, aID, aDegreeScheme); // static error here
               System.out.println("Enter another (1 for Yes/2 for No) :> ");
               int nextStudent = in.nextInt();
               if (nextStudent == 1)
               {
                   anotherStudent = false;
               }
              else if (nextStudent == 2)
              {
                   anotherStudent = true;      
              }

              else
              {
                   System.out.println("No correct number entered, exiting menu...");
                   anotherStudent = true;
              }
          }

        if (option == 2)
        {
            boolean anotherStudent2 = false;
            while (anotherStudent2 != true)
            {
                System.out.println("");
                System.out.println("Delete a Student");
                System.out.println("****************");
                System.out.println("");
                System.out.println("Enter Student ID    :> ");
                int studentID = in.nextInt();
                RegistryInterface.doDeleteStudent(studentID);  // Static error here.
                System.out.println("Delete another (1 for Yes/2 for No) :> ");
                int nextStudent2 = in.nextInt();
                if (nextStudent2 == 1)
                {
                    anotherStudent2 = false;
                }
                else if (nextStudent2 == 2)
                {
                    anotherStudent2 = true;
                }

                else
                {
                    System.out.println("No correct number entered, exiting menu...");
                    anotherStudent2 = true;
                }
               }

               if (option == 3)
               {
                   System.out.println("");
                   RegistryInterface.doPrintRegistry();  // static error here.
               }

               if (option == 4)
               {
                   menuClose = true;
               }
           }
       }
   }

   private void doAddStudent(String aForeName, String aSurName, String aID, String aDegreeScheme)
   {
       theRegistry.addStudent(new Student(aForeName, aSurName, aID, aDegreeScheme));
   }

   private void doDeleteStudent(int studentID)
   {
       theRegistry.deleteStudent(studentID);
   }

   private void doPrintRegistry()
   {
       System.out.println(theRegistry.toString());
   }
}

因此,对于第一个错误,它将是:

non-static method doAddStudent(String,String,String,String) cannot be referenced from a static context.

克服这个问题最简单的方法是什么?如果需要,可以提供其他详细信息。

改变

RegistryInterface.doDeleteStudent(studentID);  // Static error here.

 this.doDeleteStudent(studentID);

希望对你有所帮助!