Microsoft Asp.Net Ajax框架入门(7) 名称空间、类、继承、接口
VS 2008
本文介绍如何使用Microsoft Asp.Net Ajax Library编写名称空间、类、继承、接口等OO代码
1. 准备
新建一个"Ajax client library"脚本文件:Person.js
在default.aspx页面上加入ScirptManager控件:
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Scripts>
<asp:ScriptReference Path="~/Person.js" />
</Scripts>
</asp:ScriptManager>
2. namespace
在Person.js中加入代码注册我们定义的名称空间:
Type.registerNamespace("Tristan");
3. class
现在我们在Person.js中定义一个Person类:
Tristan.Person = function(name) {
this._name = name;
}
Tristan.Person.prototype = {
get_name : function() {
return this._name;
},
set_name : function(name) {
this._name = name;
},
sayHello : function() {
alert('I am a person, my name is ' + this._name);
}
}

Tristan.Person.registerClass("Tristan.Person");
编写测试代码:
var p = new Tristan.Person('guozhijian');
p.sayHello();
运行:弹出提示框 "I am a person, my name is guozhijian"
4. inheritance
现在编写一个Student类继承自Person类:
Tristan.Student = function(name) {
Tristan.Student.initializeBase(this, [name]);
}

Tristan.Student.registerClass("Tristan.Student", Tristan.Person);
两个步骤实现继承:
1) 子类构造函数中调用 initializeBase方法,第一个param为关键字this,第二个参数为构造函数参数组成的Array
2) 类代码的末尾调用 registerClass方法,标明父类
编写测试代码:
var s = new Tristan.Student("zhenglanzhen");
s.sayHello();
运行:弹出提示框 "I am a person, my name is zhenglanzhen"
现在我要override sayHello方法:
Tristan.Student.prototype = {
sayHello : function() {
alert('I am a student, my name is ' + this._name);
}
}
运行:弹出提示框 "I am a student, my name is zhenglanzhen"
改一下,我的sayHello方法要先调用一下父类的sayHello方法,然后再接着子类自己的逻辑,那么:
Tristan.Student.prototype = {
sayHello : function() {
Tristan.Student.callBaseMethod(this, 'sayHello');
alert('actually I am a student');
}
}
实现步骤:调用callBaseMethod方法,第一参数:this关键字, 第二个参数:要调用的父类的方法名,第三个参数:可选,如果父类的这个方法有参数,则为参数列表组成的Array
运行:弹出提示框 "I am a person, my name is zhenglanzhen"
弹出提示框 "actually I am a student"
5. interface
现在要让Student有行走的行为,定义一个接口 IWalkable:
Tristan.IWalkable = function() {
throw Error.notImplemented();
}
Tristan.IWalkable.prototype = {
walk : function() {
throw Error.notImplemented();
}
}
Tristan.IWalkable.registerInterface("Tristan.IWalkable");
实现步骤:
1. 方法中都抛异常,避免接口被实例化
2. 调用registerInterface方法
现在要让Student类实现IWalkable接口,对Student类做如下改动:
Tristan.Student.registerClass("Tristan.Student", Tristan.Person, Tristan.IWalkable);
标明Student类实现Tristan.IWalkable接口,如果要实现多个接口,那么继续往后加参数
编写测试代码:
var s = new Tristan.Student("zhenglanzhen");
s.walk();
运行:报错了!啊,walk方法还没有实现
对Student做如下改动:
Tristan.Student.prototype = {
sayHello : function() {
Tristan.Student.callBaseMethod(this, 'sayHello');
alert('actually I am a student');
},
walk : function() {
alert('I am walking');
}
}
再次运行测试代码,弹出提示框 "I am walking"
本文介绍如何使用Microsoft Asp.Net Ajax Library编写名称空间、类、继承、接口等OO代码
1. 准备
新建一个"Ajax client library"脚本文件:Person.js
在default.aspx页面上加入ScirptManager控件:





2. namespace
在Person.js中加入代码注册我们定义的名称空间:

3. class
现在我们在Person.js中定义一个Person类:
















编写测试代码:


4. inheritance
现在编写一个Student类继承自Person类:





两个步骤实现继承:
1) 子类构造函数中调用 initializeBase方法,第一个param为关键字this,第二个参数为构造函数参数组成的Array
2) 类代码的末尾调用 registerClass方法,标明父类
编写测试代码:


现在我要override sayHello方法:





改一下,我的sayHello方法要先调用一下父类的sayHello方法,然后再接着子类自己的逻辑,那么:






实现步骤:调用callBaseMethod方法,第一参数:this关键字, 第二个参数:要调用的父类的方法名,第三个参数:可选,如果父类的这个方法有参数,则为参数列表组成的Array
运行:弹出提示框 "I am a person, my name is zhenglanzhen"
弹出提示框 "actually I am a student"
5. interface
现在要让Student有行走的行为,定义一个接口 IWalkable:









实现步骤:
1. 方法中都抛异常,避免接口被实例化
2. 调用registerInterface方法
现在要让Student类实现IWalkable接口,对Student类做如下改动:

编写测试代码:


对Student做如下改动:








