class 语句
更新:2007 年 11 月
声明一个类的名称以及组成该类的变量、属性和方法的定义。
[modifiers] class classname [extends baseclass] [implements interfaces]{
[classmembers]
}
参数
modifiers
可选项。控制类的可见性和行为的修饰符。classname
必选。class 的名称;遵循标准的变量命名规则。extends
可选项。指示 classname 类扩展 baseclass 的关键字。如果未使用此关键字,则创建扩展 System.Object 的标准 JScript 基类。baseclass
可选项。所扩展的类的名称。implements
可选项。指示 classname 类实现一个或多个接口的关键字。interfaces
可选项。接口名称的逗号分隔列表。classmembers
可选。classmembers 可以是以下对象:用 function 语句定义的方法或构造函数声明,用 function get 和 function set 语句定义的属性声明,用 var 或 const 语句定义的字段声明,用 static 语句定义的初始值设定项声明,用 enum 语句定义的枚举声明,或者嵌套类声明。
备注
根据类的修饰符,类可用于创建实例或用作其他类的基类。如果一个类使用 abstract 修饰符来标记,该类则可用作其他要扩展的类的基类,但不能创建 abstract 类的实例。如果一个类使用 final 修饰符来标记,则可用 new 运算符来创建该类的实例,但该类不能用作基类。
方法和构造函数可能会在类中重载。因此,多个方法(或构造函数)可以具有相同的名称。重载的类成员通过它们的唯一签名来进行区分,签名包含成员的名称及其每个形参的数据类型。重载允许一个类将具有相似功能的方法归为一组。
类可以使用 extends 关键字来继承现有基类的功能。基类的方法可以通过声明与新的类方法具有相同签名的新方法来进行重写。新类中的方法可以使用 super 语句来访问基类的重写成员。
类可以使用 implements 关键字在一个或多个接口上进行模式化。由于接口不提供任何成员的实现,类不能从接口继承任何行为。接口会为类提供“签名”,签名可以在与其他类进行交互时使用。除非实现接口的类为 abstract,否则必须为接口中定义的每一个方法提供实现。
修饰符可用于使类实例的行为更像 JScript 对象的行为。若要允许类实例动态地处理所添加的属性,请使用 expando 修饰符,该修饰符会自动为类创建默认的索引属性。只有 expando 属性才能使用 JScript Object 对象的方括号来进行访问。
示例 1
下面的示例创建一个具有多个字段和方法的 CPerson 类,其细节已省略。CPerson 类用作下一个示例中 CCustomer 类的基类。
// All members of CPerson are public by default.
class CPerson{
var name : String;
var address : String;
// CPerson constuctor
function CPerson(name : String){
this.name = name;
};
// printMailingLabel is an instance method, as it uses the
// name and address information of the instance.
function printMailingLabel(){
print(name);
print(address);
};
// printBlankLabel is static as it does not require
// any person-specific information.
static function printBlankLabel(){
print("-blank-");
};
}
// Print a blank mailing label.
// Note that no CPerson object exists at this time.
CPerson.printBlankLabel();
// Create a CPerson object and add some data.
var John : CPerson = new CPerson("John Doe");
John.address = "15 Broad Street, Atlanta, GA 30315";
// Print a mailing label with John's name and address.
John.printMailingLabel();
该代码的输出为:
-blank-
John Doe
15 Broad Street, Atlanta, GA 30315
示例 2
CCustomer 类从 CPerson 派生,它具有其他不适用于 CPerson 类一般成员的字段和方法。
// Create an extension to CPerson.
class CCustomer extends CPerson{
var billingAddress : String;
var lastOrder : String;
// Constructor for this class.
function CCustomer(name : String, creditLimit : double){
super(name); // Call superclass constructor.
this.creditLimit = creditLimit;
};
// Customer's credit limit. This is a private field
// so that only member functions can change it.
private var creditLimit : double;
// A public property is needed to read the credit limit.
function get CreditLimit() : double{
return creditLimit;
}
}
// Create a new CCustomer.
var Jane : CCustomer = new CCustomer("Jane Doe",500.);
// Do something with it.
Jane.billingAddress = Jane.address = "12 Oak Street, Buffalo, NY 14201";
Jane.lastOrder = "Windows 2000 Server";
// Print the credit limit.
print(Jane.name + "'s credit limit is " + Jane.CreditLimit);
// Call a method defined in the base class.
Jane.printMailingLabel();
该部分代码的输出为:
Jane Doe's credit limit is 500
Jane Doe
12 Oak Street, Buffalo, NY 14201