声明类的实例支持 expando 属性或者方法是 expando 对象构造函数。
expando statement
- statement
必选。类或方法定义。
expando 修饰符用于将类标记为可动态扩展(支持 expando 属性的类)。expando 类实例的 expando 属性必须使用 [] 符号来进行访问;它们不能使用点运算符来进行访问。expando 修饰符还将方法标记为 expando 对象构造函数。
类和类中的方法可以使用 expando 修饰符来标记。字段、属性、接口和接口的成员不能采用 expando 修饰符。
expando 类具有一个名为 Item 的隐藏私有属性,它采用一个 Object 参数并返回 Object。不得使用 expando 类的这一签名来定义属性。
示例 1下面的示例阐释 expando 修饰符在一个类上的用法。expando 类类似于 JScript Object,但是它们具有以下所示的一些差异。
expando class CExpandoExample { var x : int = 10; } // New expando class-based object. var testClass : CExpandoExample = new CExpandoExample; // New JScript Object. var testObject : Object = new Object; // Add expando properties to both objects. testClass["x"] = "ten"; testObject["x"] = "twelve"; // Access the field of the class-based object. print(testClass.x); // Prints 10. // Access the expando property. print(testClass["x"]); // Prints ten. // Access the property of the class-based object. print(testObject.x); // Prints twelve. // Access the same property using the [] operator. print(testObject["x"]); // Prints twelve.
该代码的输出为
10 ten twelve twelve示例 2
下面的示例阐释 expando 修饰符在一个方法上的用法。以通常方法调用 expando 方法时,它访问字段 x。当使用 new 运算符将该方法用作显式构造函数时,它将 expando 属性添加到新对象。
class CExpandoExample { var x : int; expando function constructor(val : int) { this.x = val; return "Method called as a function."; } } var test : CExpandoExample = new CExpandoExample; // Call the expando method as a function. var str = test.constructor(123); print(str); // The return value is a string. print(test.x); // The value of x has changed to 123. // Call the expando method as a constructor. var obj = new test.constructor(456); // The return value is an object, not a string. print(obj.x); // The x property of the new object is 456. print(test.x); // The x property of the original object is still 123.
该代码的输出为
Method called as a function. 123 456 123要求请参见
参考
static 修饰符var 语句
function 语句
class 语句
概念
变量和常数的范围类型批注