2011-03-07 5 views

답변

6

호출은 슈퍼

class A { 
    int foo() { return 2; } 
} 

class B extends A { 

    boolean someCondition; 

    public B(boolean b) { someCondition = b; } 

    int foo() { 
     if(someCondition) return super.foo(); 
     return 3; 
    } 
} 
+0

클래스 A의 foo() 메소드 정적이어서 다음과 같이 호출 할 수 있습니다. super.foo()? –

+3

@Eng Nope! 정적은 매우 다른 것을 의미합니다. – corsiKa

6

super가 무엇이다 그. 당신이 방법 method를 오버라이드 (override)하는 경우는 다음과 같이 그것을 구현할 수 :

protected void method() { 
    if (special_conditions()) { 
     super.method(); 
    } else { 
     // do your thing 
    } 
} 
2

당신은 일반적으로 부모 클래스의 기능에 액세스 할 키워드 super를 사용할 수 있습니다. 예를 들어 [? 자바에서 어떻게 파생 클래스에서 재정의 방법에서 기본 클래스의 메서드를 호출하는]

public class Subclass extends Superclass { 

    public void printMethod() { //overrides printMethod in Superclass 
     super.printMethod(); 
     System.out.println("Printed in Subclass"); 
    } 
    public static void main(String[] args) { 

    Subclass s = new Subclass(); 
    s.printMethod();  
    } 

} 

http://download.oracle.com/javase/tutorial/java/IandI/super.html에서 촬영

관련 문제