剖析ASP.NET AJAX的面向对象思想(3)
Inheritance.js脚本文件中定义了两个类:Person和Employee,Employee是从Person继承而来。每个类都有字段、公共属性和方法。另外,Employee类重写了toString的实现,并在重写的代码中调用了基类的功能。在这个例子中把类Person的名字空间设定为"Demo"。运行页面Inheritance.aspx,点击“创建对象”、“对象释放”、“公共和私有属性”、“对象方法”、“重写方法”,“对象类型检查”体验一下。
4.接口
接口是类要实现的逻辑协议,是对类进行集成的公共遵守的规范。它能使多个类和同一个接口把实现定义和类的具体实现结合起来。下面的例子定义了一个基类Tree和接口IFruitTree,Apple和Banana这两个类实现了接口IFruitTree,但Pine类没有实现接口IFruitTree。
Type.registerNamespace("Demo.Trees");
Demo.Trees.IFruitTree = function() {} Demo.Trees.IFruitTree.Prototype = { bearFruit: function(){} } Demo.Trees.IFruitTree.registerInterface('Demo.Trees.IFruitTree'); Demo.Trees.Tree = function(name) { this._name = name; } Demo.Trees.Tree.prototype = { returnName: function() { return this._name; }, toStringCustom: function() { return this.returnName(); }, makeLeaves: function() {} } Demo.Trees.Tree.registerClass('Demo.Trees.Tree'); Demo.Trees.FruitTree = function(name, description) { Demo.Trees.FruitTree.initializeBase(this, [name]); this._description = description; } Demo.Trees.FruitTree.prototype.bearFruit = function() { return this._description; } Demo.Trees.FruitTree.registerClass('Demo.Trees.FruitTree', Demo.Trees.Tree, Demo.Trees.IFruitTree); Demo.Trees.Apple = function() { Demo.Trees.Apple.initializeBase(this, ['Apple', 'red and crunchy']); } Demo.Trees.Apple.prototype = { makeLeaves: function() { alert('Medium-sized and desiduous'); }, toStringCustom: function() { return 'FruitTree ' + Demo.Trees.Apple.callBaseMethod(this, 'toStringCustom'); } } Demo.Trees.Apple.registerClass('Demo.Trees.Apple', Demo.Trees.FruitTree); Demo.Trees.GrannySmith = function() { Demo.Trees.GrannySmith.initializeBase(this); // You must set the _description feild after initializeBase // or you will get the base value. this._description = 'green and sour'; } Demo.Trees.GrannySmith.prototype.toStringCustom = function() { return Demo.Trees.GrannySmith.callBaseMethod(this, 'toStringCustom') + ' ... its GrannySmith!'; } Demo.Trees.GrannySmith.registerClass('Demo.Trees.GrannySmith', Demo.Trees.Apple); Demo.Trees.Banana = function(description) { Demo.Trees.Banana.initializeBase(this, ['Banana', 'yellow and squishy']); } Demo.Trees.Banana.prototype.makeLeaves = function() { alert('Big and green'); } Demo.Trees.Banana.registerClass('Demo.Trees.Banana', Demo.Trees.FruitTree); Demo.Trees.Pine = function() { Demo.Trees.Pine.initializeBase(this, ['Pine']); } Demo.Trees.Pine.prototype.makeLeaves = function() { alert('Needles in clusters'); } Demo.Trees.Pine.registerClass('Demo.Trees.Pine', Demo.Trees.Tree); |
Interface.js脚本文件中定义了一个Tree基类和一个IFruitTree接口。Apple和Banana两个继承类实现了IFruitTree接口,而Pine类没有实现IFruitTree接口。运行Interface.aspx,点击“对象创建”、“接口检查”、“调用接口方法”体验一下。
5.枚举
枚举是包含一组被命名的正整数常数的类。你可以像访问属性一样访问它的值。例如:
myObject.color = myColorEnum.red,枚举提供了一种很容易理解的整数表示。下面的例子定义了一个以十六进制数表示的颜色被命名为有意义的名字的枚举类型。
运行Enumeration.aspx,选择下拉框中的颜色,脚本程序把背景色设为选中的枚举类型Demo.Color中的颜色。
6.反射
反射用于检查一个运行期程序的结构和组成,是通过类Type的API来实现反射的,这些方法使你能够收集一个对象的信息,例如:它是继承于哪个类、它是否实现类某个特指的接口、它是否是某个类的实例等。
下面的例子用反射的API测试GrannySmith类是否实现了前面的接口。运行Reflection.aspx,点击“检查类型”、“检查继承”、“检查接口”体验一下。
另外需要说明的一点是,Microsoft AJAX Library是基于开放体系架构而开发的,不仅仅用于asp.net,还能用于其它体系架构中,例如用在java中。