共用方式為


super 陳述式

更新:2007 年 11 月

可參考目前物件的基底物件。這可用於兩種內容。

 // 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 陳述式