코드에서 정적 메소드를 제거하는 방법 (및 교체하는 방법)

Wendela Lövgren :

난 당신이 알다시피 있도록 프로그래밍 및 자바 아주 새로운 해요 ...

나는 다음과 같은 설명을 구현하고 명령 제어 프로그램의 골격을 테스트있는 코드 / 프로그램 골격 (학교 과제)에 일하고 있어요 그리고 개 하루 개를 추적하는 데 사용하기위한 것입니다, 그리고 프로그램이 첫 번째 단계에서 수용해야하는 명령은 다음과 같습니다 -register 새로운 개 늘립니다 시대 -list 개 -remove 개 -exit

제대로 작동합니다 이러한 명령의 하나는 프로그램이 종료되었다는 메시지를 출력하고 프로그램을 종료한다 종료합니다. 이 작업은 사용할 수 없습니다을 System.exit에 의해 명령 행을 폐쇄, 그리고 수행해야합니다. 다른 명령은 지정하는 명령을 알려주는 짧은 텍스트를 인쇄해야합니다. 이 텍스트는 테스트 프로그램을 식별 할 수 있도록 위와 같이 전체 명령 이름을 포함해야합니다. 하나의 팁은 짧은, 자신의 테스트를 용이하게되도록 명령, 다른 동의하는 것입니다. "

비 기능적 요구 사항 중 하나는 정적 메소드 나 변수가 메인 캐릭터를 제외하고 사용되지 않을 수도 있다는 것이다.

내 질문은 그러므로; 어떻게 내 프로그램의 골격에서 정적 메소드를 제거 할 수 있습니까? 나는 정말 힘든 시간이 이해하기!

import java.util.Scanner;

public class ProgramSkeleton {

    static Scanner input = new Scanner(System.in);

    public static void initialize() {
        System.out.println("Welcome to the dog register!");
        System.out.println("Write 0 to register new dog");
        System.out.println("Write 1 to increase age");
        System.out.println("Write 2 to list dogs");
        System.out.println("Write 3 to remove dog");
        System.out.println("Write 4 to exit");
    }

    public static void runCommandLoop() {
        boolean done;
        do {
            String command = readCommand();
            done = handleCommand(command);
        } while (!done);
    }

    public static String readCommand() {
        System.out.print("> ");
        String command = input.nextLine();
        return command;
    }

    private static boolean handleCommand(String command) {
        switch (command) {
        case "0":
        case "register new dog":
            System.out.println("You have chosen register new dog.");
            return true;
        case "1":
        case "increase age":
            System.out.println("You have chosen increase age.");
            return true;
        case "2":
        case "list dogs":
            System.out.println("You have chosen list dogs.");
            return true;
        case "3":
        case "remove dog":
            System.out.println("You have chosen remove dog.");
            return true;
        case "4":
            break;
        default:
            System.out.println("unknown command");
            return false;
        }
        return false;
    }

    public static void closeDown() {
        System.out.println("Goodbye!");
    }

    public static void main(String[] args) {
        initialize();
        runCommandLoop();
        closeDown();
    }

}
영혼의 거장 :

음 ... 당신은 필요 개체를 그렇지 않은 경우에 메소드를 호출하는 정적 .

이 말을하는 데, 간단히 제거 static주요 방법을 제외하고, 클래스에 키워드를. 그런 다음이 주요 방법을 변경 :

public static void main(String[] args) {
    ProgramSkeleton program = new ProgramSkeleton();
    program.initialize();
    program.runCommandLoop();
    program.closeDown();
}

추천

출처http://43.154.161.224:23101/article/api/json?id=239061&siteId=1