无法从SpecialAssignment1类型对非静态方法printMenuGetSelection()进行静态引用

时间:2014-06-05 03:55:32

标签: java debugging non-static static

预编辑:问题是当我将其标记为静态时,所以

public static int printMenuGetSelection()

它给了我消息

This Static method cannot hide the instance method from AMenu

我正在编写一个读取文件的Java程序,并为用户提供了多个显示文件内容的选项。我目前正在编写一个实现实际接口的菜单界面,使程序更易于使用。但是,当我尝试在main方法中调用menu方法时,我收到异常。错误是在main方法中的一个活动行上,我调用printMenuGetSelection(),它说

  

无法从类型SpecialAssignment1

中对非静态方法printMenuGetSelection()进行静态引用

如何修复此错误?这是我的计划:

import java.util.*;
import java.io.*;
import java.text.*;

public class SpecialAssignment1 implements AMenu {
public static void main(String[] args) throws FileNotFoundException{
    printMenuGetSelection();
    /*System.out.println(RewardCustomer("transactions1.dat")); //CURRENTLY DISPLAYING TOP 6, DOESN'T WORK WITH TIES OR TOPN < lines
    ProcessTransactionsFile("transactions2.dat", 52);*/
}
public int printMenuGetSelection() throws FileNotFoundException{
    boolean runProgram = true;
    Scanner s = new Scanner(System.in);
    printStartMenu();
    String startMenuSelection = s.next();
    while(runProgram){
        if(startMenuSelection.equals("1")){
            startMenu1();
        } else if(startMenuSelection.equals("2")){
            startMenu2();
        } else if(startMenuSelection.equals("3")){
            startMenu3();
        } else if(startMenuSelection.equals("4")){
            startMenu4();
        } else if(startMenuSelection.equals("5")){
            runProgram = false;
        } else {
            System.out.println("***Selection Invalid!***");
        }
    }
    return 1;
}

public static void printStartMenu(){
    System.out.println("**********************************************************");
    System.out.println("Main Menu...");
    System.out.println("    (1)  RewardCustomers");
    System.out.println("    (2)  ProcessTransactionFiles");
    System.out.println("    (3)  TopCustomers");
    System.out.println("    (4)  QueryStatsFile");
    System.out.println("    (5)  Quit");
    System.out.println("        Enter a valid selection: ");
    }

public static void startMenu1() throws FileNotFoundException{
    boolean runMenu1 = true;
    while(runMenu1){
        Scanner s = new Scanner(System.in);
        System.out.println("Reward Customers Menu...");
        System.out.println("    (1)  Use transactions1.dat");
        System.out.println("    (2)  Use transactions2.dat");
        System.out.println("    (3)  Quit");
        System.out.println("        Enter a valid selection: ");
        String menu1Selection = s.next();
        if(menu1Selection.equals("1")){
            System.out.println(RewardCustomer("transactions1.dat"));
        } else if(menu1Selection.equals("2")){
            System.out.println(RewardCustomer("transactions2.dat"));
        } else if(menu1Selection.equals("3")){
            runMenu1 = false;
        } else {
            System.out.println("***Selection Invalid!***");
        }
    }
}

public static void startMenu2(){
    boolean runMenu2 = true;
    while(runMenu2){
        Scanner s = new Scanner(System.in);
        System.out.println("Process Transaction Files Menu...");
        System.out.println("    (1)  Create transactions2.dat file");
        System.out.println("    (2)  Display transactions1.dat");
        System.out.println("    (3)  Display transactions2.dat");
        System.out.println("    (4)  Query transactions1.dat");
        System.out.println("    (5)  Query transactions2.dat");
        System.out.println("    (6)  Quit");
        System.out.println("        Enter a valid selection: 4");
        String menu2Selection = s.next();
        if(menu2Selection.equals("1")){

        }
    }
}

public static void startMenu3(){

}

public static void startMenu4(){

}

我删除了与问题无关的代码,以便于阅读,如果需要,我会将其放入。此外,这里是AMenu界面。请不要对我的程序进行任何其他更改。如果您觉得将菜单作为实施界面愚蠢,我100%同意您的意见,但这是要求。作为参考,这是AMenu接口:

import java.io.FileNotFoundException;

public interface AMenu {
/**
 * Prints a menu with selections and logic to return a valid selection.
 * @return the selected item
 */
abstract int printMenuGetSelection() throws FileNotFoundException;

 /**
 * @return the numberOfMenuItems
 */
abstract int getNumberOfMenuItems();
}

3 个答案:

答案 0 :(得分:0)

由于printMenuGetSelection()是非静态的,因此除非您创建main()的实例并在该对象上调用该方法,否则无法在静态方法SpecialAssignment1内调用它。

答案 1 :(得分:0)

正如其他人所说,您需要创建SpecialAssignment1的实例,然后在其上调用printMenuSelection()。令人困惑的部分原因是您已将主要方法粘贴在菜单界面类中。如果你有一个只有main方法的类SpecialAssignment1和一个包含所有菜单生成内容的单独的MenuGenerator类,那么整个事情会更有意义。

答案 2 :(得分:0)

您需要创建SpecialAssignment1的实例,然后从中调用该方法,因为抽象需要一个对象。