2011-01-03 8 views
1

가능한 중복 : 내가 튜브/지하철 티켓 시스템에 대한 인터페이스 클래스를 만들려고 노력하고
When should a method be static?정적 및 비 정적 메서드

. 물론 현실은 아니지만 컴퓨터 과학 모듈의 교과 과정에 적합합니다. 정적 메서드를 사용해야 할 때 이해가 안됩니다. 컴퓨터 과학에 대해 많이 알지 못하지만 주요 방법은 정적 인 것 같습니다.

class UNInterfaceTest 
{ 
    public static final int NOTTING_HILL = 1; 
    public static final int HIGH_KEN = 2; 
    public static final int GLOUS = 3; 
    public static final int SOUTH_KEN = 4; 
    public static final int SLOANE = 5; 
    public static final int VICTORIA = 6; 
    public static final int ST_JAMES = 7; 
    public static final int WESTMINSTER = 8; 
    public static final int QUIT = 10; 
    private Input in = new Input(); 

private static void displayMenu() 
{ 
    System.out.println("CIRCLE LINE: Please Select the Number of Your Current Station."); 
    System.out.println(NOTTING_HILL + ": Nottinghill_Gate"); 
    System.out.println(HIGH_KEN + ": High_Street_Kensignton"); 
    System.out.println(GLOUS + ": Gloucester_Road"); 
    System.out.println(SOUTH_KEN + ": South_Kensignton"); 
    System.out.println(SLOANE + ": Sloane_Square"); 
    System.out.println(VICTORIA + ": Victoria"); 
    System.out.println(ST_JAMES + ": St_James_Park"); 
    System.out.println(WESTMINSTER + ": Westminster"); 
    System.out.println(); 
    System.out.println(QUIT + ". Quit"); 
} 

public static void run() 
{ 
    while(true) 
    { 
     displayMenu(); 
     int option = getMenuInput(); 
     if (option == QUIT) 
    { 
     break; 
    } 
    doOption(option); 
    } 
} 

private static void doOption(int option) 
{ 
    switch(option){ 
case NOTTING_HILL: 
    //findNottinghill_Gate(); 
     break; 
     case HIGH_KEN: 
    //findHighStreetKensignton(); 
     break; 
     case GLOUS: 
    //findGloucesterRoad(); 
     break; 
     case SOUTH_KEN: 
    //findSouthKensignton(); 
     break; 
     case SLOANE: 
    // findSloaneSquare(); 
     break; 
     case VICTORIA: 
    //findVictoria(); 
     break; 
     case ST_JAMES: 
    //findStJamesPark(); 
     break; 
     case WESTMINSTER: 
     //findWestminster(); 
     break; 
     default: 
     System.out.println("Invalid option - try again"); 
    } 
} 
private int getMenuInput() 
{ 
    //KeyboardInput val = new KeyboardInput(); 
    System.out.print("Enter menu selection: "); 
    int option = in.nextInt(); 
    in.nextLine(); 
    return option; 
} 

public static void main(String[] args) 
{ 
    run(); 
} 
} 

답변

0

인스턴스 메서드는 티켓 기계이며 정적 메서드는 모든 티켓 기계에 동시에 영향을 미치는 작업을 수행합니다.

이제 귀하의 경우에 한 번에 e에서만 사실적으로 사용할 수 있습니다. 그러나 개념 상 각 머신은 자체 키보드, 스크린 및 CPU를 가지므로 모든 메소드가 인스턴스 여야합니다. 당신이 main에 기계를 만들 필요가 의미

: 물론

UNInterfaceTest machine = new UNInterfaceTest(); 
machine.run(); 

, 당신은 현재 (Input가에서 무엇을 읽고 확실하지) 표준 출력에 직접 쓰고있어. 더 나은 디자인은 Writer을 UNInterfaceTest 생성자에 전달한 다음 System.out 대신에이 값을 씁니다. 이를 통해 각 기계는 자체 화면을 가질 수 있습니다.

물론이 정보 중 일부는 할당 범위를 벗어날 수 있습니다. 알아두면 유용 할 것입니다.

2

static 메서드는 클래스의 인스턴스와 관련되지 않습니다.
정의 된 클래스의 인스턴스를 사용하거나 수정하지 않는 메소드는 모두 static이어야합니다.

0

짧은 대답 : 당신이 당신의 물건을 사용하고 있지 않기 때문입니다. 당신은 개체로 클래스를 인스턴스화하고 비 정적 방법을 사용하여

 

UNInterfaceTest unit = new UNInterfaceTest(); 
unit.run(); 
 

사용하여 실행할 수 있습니다.

5

클래스를 쿠키 커터로 생각하고 개체를 쿠키 (클래스의 인스턴스)로 생각하십시오. 정적 메소드는 MyClass.myMethod();에서와 같이 클래스 자체의 메소드 (쿠키 커터)이며, 비 정적 메소드는 new MyClass().myMethod();과 같이 클래스 인스턴스 (쿠키)의 일부입니다.

귀하의 경우, 정적 방법은 얼마나 많은 돈을 공제해야하는지 계산하는 것과 같은 일반적으로 티켓 기계에 한정되는 것입니다. 비 정적 메서드는 처리 된 티켓 수를 추적하는 등 단일 티켓 카운터에만 해당하는 것입니다. http://cscie160-distance.com/nonstatic.html

+1

1
, 나는 비유처럼 :

여기에 좀 더 정보입니다. – Sapph

관련 문제