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.