super 문

현재 개체의 기본 개체를 참조합니다. 이 문은 두 가지 컨텍스트에서 사용될 수 있습니다.

// Syntax 1: Calls the base-class constructor with arguments.
super(arguments)

// Syntax 2: Accesses a member of the base class.
super.member

인수

  • arguments
    구문 1에서는 선택적 요소로, 기본 클래스 생성자 인수의 쉼표로 구분된 목록입니다.

  • member
    구문 2에서는 필수적 요소로, 액세스할 기본 클래스의 멤버를 나타냅니다.

설명

super 키워드는 일반적으로 두 가지 경우에 사용됩니다. 하나 이상의 인수로 기본 클래스 생성자를 명시적으로 호출할 때 사용하거나 현재 클래스에 의해 재정의된 기본 클래스 멤버에 액세스하는 데 사용할 수 있습니다.

예제 1

다음 예제에서 super는 기본 클래스의 생성자를 참조합니다.

class baseClass {
   function baseClass() {
      print("Base class constructor with no parameters.");
   }
   function baseClass(i : int) {
      print("Base class constructor. i is "+i);
   }
}
class derivedClass extends baseClass {
   function derivedClass() {
      // The super constructor with no arguments is implicitly called here.
      print("This is the derived class constructor.");
   }
   function derivedClass(i : int) {
      super(i);
      print("This is the derived class constructor.");
   }
}

new derivedClass;
new derivedClass(42);

이 프로그램을 실행하면 다음과 같이 출력됩니다.

Base class constructor with no parameters.
This is the derived class constructor.
Base class constructor. i is 42
This is the derived class constructor.

예제 2

다음 예제에서는 super를 통해 기본 클래스의 재정의된 멤버에 액세스합니다.

class baseClass {
   function test() {
      print("This is the base class test.");
   }
}
class derivedClass extends baseClass {
   function test() {
      print("This is the derived class test.");
      super.test(); // Call the base class test.
   }
}

var obj : derivedClass = new derivedClass;
obj.test();

이 프로그램을 실행하면 다음과 같이 출력됩니다.

This is the derived class test.
This is the base class test.

요구 사항

.NET 버전

참고 항목

참조

new 연산자

this 문