接口
接口是类要实现的逻辑协议是对类进行集成的公共遵守的规范它能使多个类和同一个接口把实现定义和类的具体实现结合起来下面的例子定义了一个基类Tree和接口IFruitTreeApple和Banana这两个类实现了接口IFruitTree但Pine类没有实现接口IFruitTree
TyperegisterNamespace(DemoTrees);
DemoTreesIFruitTree = function() {}
DemoTreesIFruitTreePrototype = {
bearFruit: function(){}
}
DemoTreesIFruitTreeregisterInterface(DemoTreesIFruitTree);
DemoTreesTree = function(name) {
this_name = name;
}
DemoTreesTreeprototype = {
returnName: function() {
return this_name;
}
toStringCustom: function() {
return thisreturnName();
}
makeLeaves: function() {}
}
DemoTreesTreeregisterClass(DemoTreesTree);
DemoTreesFruitTree = function(name description) {
DemoTreesFruitTreeinitializeBase(this [name]);
this_description = description;
}
DemoTreesFruitTreeprototypebearFruit = function() {
return this_description;
}
DemoTreesFruitTreeregisterClass(DemoTreesFruitTree DemoTreesTree DemoTreesIFruitTree);
DemoTreesApple = function() {
DemoTreesAppleinitializeBase(this [Apple red and crunchy]);
}
DemoTreesAppleprototype = {
makeLeaves: function() {
alert(Mediumsized and desiduous);
}
toStringCustom: function() {
return FruitTree + DemoTreesApplecallBaseMethod(this toStringCustom);
}
}
DemoTreesAppleregisterClass(DemoTreesApple DemoTreesFruitTree);
DemoTreesGrannySmith = function() {
DemoTreesGrannySmithinitializeBase(this);
// You must set the _description feild after initializeBase
// or you will get the base value
this_description = green and sour;
}
DemoTreesGrannySmithprototypetoStringCustom = function() {
return DemoTreesGrannySmithcallBaseMethod(this toStringCustom) + its GrannySmith!;
}
DemoTreesGrannySmithregisterClass(DemoTreesGrannySmith DemoTreesApple);
DemoTreesBanana = function(description) {
DemoTreesBananainitializeBase(this [Banana yellow and squishy]);
}
DemoTreesBananaprototypemakeLeaves = function() {
alert(Big and green);
}
DemoTreesBananaregisterClass(DemoTreesBanana DemoTreesFruitTree);
DemoTreesPine = function() {
DemoTreesPineinitializeBase(this [Pine]);
}
DemoTreesPineprototypemakeLeaves = function() {
alert(Needles in clusters);
}
DemoTreesPineregisterClass(DemoTreesPine DemoTreesTree);
Interfacejs脚本文件中定义了一个Tree基类和一个IFruitTree接口Apple和Banana两个继承类实现了IFruitTree接口而Pine类没有实现IFruitTree接口运行Interfaceaspx点击对象创建接口检查调用接口方法体验一下
枚举
枚举是包含一组被命名的正整数常数的类你可以像访问属性一样访问它的值例如myObjectcolor = myColorEnumred枚举提供了一种很容易理解的整数表示下面的例子定义了一个以十六进制数表示的颜色被命名为有意义的名字的枚举类型
TyperegisterNamespace(Demo);
// Define an enumeration type and register it
DemoColor = function(){};
DemoColorprototype =
{
Red: xFF
Blue: xFF
Green: xFF
White: xFFFFFF
}
DemoColorregisterEnum(DemoColor);
运行Enumerationaspx选择下拉框中的颜色脚本程序把背景色设为选中的枚举类型DemoColor中的颜色
反射
反射用于检查一个运行期程序的结构和组成是通过类Type的API来实现反射的这些方法使你能够收集一个对象的信息例如它是继承于哪个类它是否实现类某个特指的接口它是否是某个类的实例等
下面的例子用反射的API测试GrannySmith类是否实现了前面的接口运行Reflectionaspx点击检查类型检查继承检查接口体验一下
另外需要说明的一点是Microsoft AJAX Library是基于开放体系架构而开发的不仅仅用于aspnet还能用于其它体系架构中例如用在java中
[] [] []