java - 不能从具有方法和变量的静态上下文中引用非静态方法

标签 java methods static compiler-errors

<分区>

在编写 BookStoreApplication 时,它使用 Book、Tape 和 CD 类来创建对象。 虽然未完成,但应用程序类应该创建新的 BookStoreItems,即 Book、Tape 和 CD。它们继承自 BookStoreItems 类。 在这个应用程序类中,我不断收到错误:

error: non-static method printMenu() cannot be referenced from a static context
error: non-static method getUserChoice() cannot be referenced from a static context
error: non-static variable input cannot be referenced from a static context

我已经将其更改为静态,然后又不是静态,但我仍然收到此错误...

import java.util.Scanner;

public class BookStoreApp2 {

    //constants for options
    static final int ADD_BOOK = 0;
    static final int ADD_TAPE = 1;
    static final int ADD_CD = 2;
    static final int QUIT = -1;

    Scanner input = new Scanner (System.in);

    public static void main(String[] args) {


        BookStoreItem[] item;//declaring array

        item = new BookStoreItem[10];//initializing array

        int itemType = -1;

        printMenu();
        getUserChoice();

        for (int i = 0; i < item.length; i++){
            System.out.print("\n" + i + "\tEnter 0 for Book, 1 for Tape, 2 for CD: ");
            itemType = input.nextInt();

            switch (itemType) {
                case 0:
                    item[i] = new Book();
                    break;
                case 1:
                    item[i] = new Tape();
                    break;
                case 2:
                    item[i] = new CD();
                    break;
                default: 
                    System.out.println("\nInvalid choice.");
            }//end of switch statement

        }//end of for loop

        for (int i = 0; i < item.length; i++) {
            System.out.println("\nAnimal #" + i + ": ");

            System.out.println("\n\tTitle: " + item[i].getTitle()); //polymorphic because they can operate on separate objects
            System.out.println("\n\tAuthor: " + item[i].getAuthor());
        }//end of for


    }//end of main method


//PRINT MENU----------------------------------------------------------  
    public void printMenu(){
        System.out.println("\nPress:");
        System.out.println("\t" + ADD_BOOK + "\tTo add a book to the book store.\n");
        System.out.println("\t" + ADD_TAPE + "\tTo add a tape to the book store.\n");
        System.out.println("\t" + ADD_CD + "\tTo add a CD to the book store.\n");
        System.out.println("\t" + QUIT + "\tTo exit\n");
    }
//---------------------------------------------------------------------
//GET USER CHOICE------------------------------------------------------ 
     public int getUserChoice() {
        int choice;     
        System.out.print("Please enter your choice: ");
        choice = input.nextInt();

        return choice;
    }//end of getUserChoice
//----------------------------------------------------------------------

}//end class

最佳答案

你需要让你的方法 - printMenu()getUserChoice() static,因为你直接从你的 static main 方法,而不创建类的实例,这些方法在其中定义。并且您不能在没有引用它们的类的实例的情况下调用 non-static 方法定义在.

或者,您可以将方法调用部分更改为:

BookStoreApp2 bookStoreApp = new BookStoreApp2();
bookStoreApp.printMenu();
bookStoreApp.getUserChoice();

关于java - 不能从具有方法和变量的静态上下文中引用非静态方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14862306/

相关文章:

java - 在java中,可以在不使用数组的情况下在另一个方法的循环中调用/调用一个方法吗?

php - PHP 接口(interface)有属性吗?

C++ 删除静态数据

Java聊天室程序

java | CXF| JAX-RS |异常处理

java - Log4j2:动态创建多个日志的日志文件

java - 数组项目似乎只打印出null

C++ map<char, static method pointer>?

将xls数据导入mysql数据库的java代码

java - Iterator for Java Framework Collection 的不同结果