super ステートメント

現在のオブジェクトの基本オブジェクトを参照します。 このステートメントは、次の 2 つのコンテキストで使用できます。

// 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 キーワードは、一般的に、次の 2 つの状況のどちらかで使用します。 1 つ以上の引数を指定して、明示的に基本クラスのコンストラクターを呼び出します。 または、現在のクラスでオーバーライドされている基本クラスのメンバーにアクセスします。

例 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 ステートメント