继承
继承是一个类派生于另一个类的能力派生类自动继承基类的所有字段属性方法和事件派生类可以增加新的成员或者重写基类已存在的成员来改变成员的行为
下面的脚本实例有两个类Person和EmployeeEmployee从Person继承而来两个类示范了私有字段的使用它们都有公共属性方法另外Employee类重写了Person类的toString实现并调用了基类的功能
TyperegisterNamespace(Demo);
DemoPerson = function(firstName lastName emailAddress) {
this_firstName = firstName;
this_lastName = lastName;
this_emailAddress = emailAddress;
}
DemoPersonprototype = {
getFirstName: function() {
return this_firstName;
}
getLastName: function() {
return this_lastName;
}
getEmailAddress: function() {
return this_emailAddress;
}
setEmailAddress: function(emailAddress) {
this_emailAddress = emailAddress;
}
getName: function() {
return this_firstName + + this_lastName;
}
dispose: function() {
alert(bye + thisgetName());
}
sendMail: function() {
var emailAddress = thisgetEmailAddress();
if (emailAddressindexOf(@) < ) {
emailAddress = emailAddress + @examplecom;
}
alert(Sending mail to + emailAddress + );
}
toString: function() {
return thisgetName() + ( + thisgetEmailAddress() + );
}
}
DemoPersonregisterClass(DemoPerson null SysIDisposable);
DemoEmployee = function(firstName lastName emailAddress team title) {
DemoEmployeeinitializeBase(this [firstName lastName emailAddress]);
this_team = team;
this_title = title;
}
DemoEmployeeprototype = {
getTeam: function() {
return this_team;
}
setTeam: function(team) {
this_team = team;
}
getTitle: function() {
return this_title;
}
setTitle: function(title) {
this_title = title;
}
toString: function() {
return DemoEmployeecallBaseMethod(this toString) + \r\n + thisgetTitle() + \r\n + thisgetTeam();
}
}
DemoEmployeeregisterClass(DemoEmployee DemoPerson);
Inheritancejs脚本文件中定义了两个类Person和EmployeeEmployee是从Person继承而来每个类都有字段公共属性和方法另外Employee类重写了toString的实现并在重写的代码中调用了基类的功能在这个例子中把类Person的名字空间设定为Demo运行页面Inheritanceaspx点击创建对象对象释放公共和私有属性对象方法重写方法对象类型检查体验一下
[] [] []