使用另一个文件夹中的类 - JAVA

时间:2018-02-06 17:04:42

标签: java class reflection import

我有一个没有main(Call.java)的java文件和一组方法。我有另一个java文件,我想在Call.java中调用构造函数。这是结构: link to file structure

我使用命令行来运行java程序。

我的问题是:如何在不收到错误的情况下这样做

源代码: 类:

import java.util.*;

public class Call{
    private int userTypedInt;
    Scanner userInput = new Scanner(System.in);

    Call(){
        while(true){
            mainMenu();
        }
    }

    public void mainMenu(){
        System.out.println("\n\t\t####| Main Menu |#### \n");
        System.out.println("\t\t0. Exit the program");
        System.out.println("\t\t1. Option");

        // check that the user typed a number - specifically an int 
        try{
            System.out.print("\nEnter a choice: ");
            userTypedInt = userInput.nextInt();
            path(userTypedInt);
        }catch(InputMismatchException e){
            System.out.println("\n\t\tInvalid entry");
            userInput.nextLine(); // clears the buffer
        }
    }

    public void path(int pick){
        switch(pick){
            case 0:
                userInput.close();
                System.exit(0);
                break;
            case 1:
                break;
            default:
                System.out.println("\t\tInvalid Entry");
        }
    }
}

测试文件:

public class Test{
    public static void main(String[] args){
        System.out.println("Online");
        new Call();

    }
}

0 个答案:

没有答案