2012-11-09 5 views
0

이 질문은 어리석은 것 같지만 마음에 분명히 들어야합니다.동적 바인딩 및 정적 바인딩

class J_SuperClass { 



void mb_method() { 
    System.out.println("J_SuperClass::mb_method"); 
    } 
    static void mb_methodStatic() { 
     System.out.println("J_SuperClass::mb_methodStatic"); 
    } 
} 
public class J_Test extends J_SuperClass { 
    void mb_method() { 
    System.out.println("J_Test::mb_method"); 
    } 
    static void mb_methodStatic() { 
    System.out.println("J_Test::mb_methodStatic"); 
    } 
    public static void main(String[] args) { 
    J_SuperClass a = new J_Test(); 
    a.mb_method(); 
    a.mb_methodStatic(); 
    J_Test b = new J_Test(); 
    b.mb_method(); 
    b.mb_methodStatic(); 
    } 
} 

출력은 다음과 같습니다

J_Test::mb_method 
J_SuperClass::mb_methodStatic 
J_Test::mb_method 
J_Test::mb_methodStatic 

나는 동적 바인딩이 런타임에 발생하고 정적 바인딩이 컴파일시에 발생하는 것을 알고있다. 또한 동적 바인딩의 경우 객체의 실제 유형에 따라 호출되는 메소드가 결정됩니다. 그래서 내 질문에 위의 코드에서 단어 "정적"정적 바인딩 및 따라서 개체의 DECLARED 형식 호출되는 메서드를 결정합니다?

+0

@ user1109363 Re : 귀하의 소금에 관한 질문을 한 번보세요 [여기] (http://stackoverflow.com/questions/12724935/salt-and-passwords) 및 [here] (http://stackoverflow.com/questions/420843/how-does-password-salt-help-a-rainbow-table-attack) – StuartLC

답변

1

정적 메서드의 경우에는 다형성이 없으므로 (정적 바인딩이 필요함) 정적이라는 단어가 정적 바인딩을 결정합니다. 이 메서드는 그 클래스에 그 이름을 가진 유일한 메서드이기 때문에 형식이 아닌 이름에 바인딩됩니다. 그것은 정적이기 때문에 어떤 대상과도 관련이 없습니다.

0

J_Test 클래스를 디 컴파일 할 수도 있습니다. 여기에 다음이 표시됩니다.

public static void main(String args[]) 
    { 
     J_SuperClass a = new J_Test(); 
     a.mb_method(); 
     J_SuperClass.mb_methodStatic(); 
     J_Test b = new J_Test(); 
     b.mb_method(); 
     mb_methodStatic(); 
    } 

따라서 수퍼 클래스의 정적 메서드가 바인딩됩니다.