繼承
繼承是一個類派生於另一個類的能力派生類自動繼承基類的所有字段屬性方法和事件派生類可以增加新的成員或者重寫基類已存在的成員來改變成員的行為
下面的腳本實例有兩個類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點擊創建對象對象釋放公共和私有屬性對象方法重寫方法對象類型檢查體驗一下
[] [] []
From:http://tw.wingwit.com/Article/program/net/201311/15064.html