在Javascript中并没有空间、类、接口这些概念,Atlas对这些东西实现封装了,增强了JavaScript面向对象方面的能力,本文看一下如何使用继承。
主要内容
1.概述
2.完整示例
一.概述
在Javascript中并没有空间、类、接口这些概念,Atlas对这些东西实现封装了,增强了JavaScript面向对象方面的能力,本文看一下如何使用继承。简单定义一个可被继承的基类,在注册类时指定类名就可以了:
BaseClass = function()
{
// ……
}
BaseClass.registerClass("BaseClass");
{
// ……
}
BaseClass.registerClass("BaseClass");
定义一个继承类,先要调用父类的构造器,除了传递本身之外还可以传递一些参数,注册类时需要指定继承自哪个类:
DerivedClass = function()
{
// ……
DerivedClass.intializeBase(this,arguments);
}
DerivedClass.registerClass("DerivedClass","BaseClass");
{
// ……
DerivedClass.intializeBase(this,arguments);
}
DerivedClass.registerClass("DerivedClass","BaseClass");
二.完整示例
看一下Atlas官方网站提供的例子,新建Atlas Web Site,添加一个Inheritance.js的JS文件,定义Person 和Employee两个类 ,并且让Employee继承于Person,Employee覆写父类中的toString方法:
// JScript File
Type.registerNamespace("Demo");
Demo.Person = function(firstName, lastName, emailAddress) {
var _firstName = firstName;
var _lastName = lastName;
var _emailAddress = emailAddress;
this.getFirstName = function() {
return _firstName;
}
this.getLastName = function() {
return _lastName;
}
this.getEmailAddress = function() {
return _emailAddress;
}
this.setEmailAddress = function(emailAddress) {
_emailAddress = emailAddress;
}
this.getName = function() {
return _firstName + ' ' + _lastName;
}
this.dispose = function() {
alert('bye ' + this.getName());
}
}
Demo.Person.registerClass('Demo.Person', null, Sys.IDisposable);
Demo.Person.prototype.sendMail = function() {
var emailAddress = this.getEmailAddress();
if (emailAddress.indexOf('@') < 0) {
emailAddress = emailAddress + '@example.com';
}
alert('Sending mail to ' + emailAddress + ' ');
}
Demo.Person.prototype.toString = function() {
return this.getName() + ' (' + this.getEmailAddress() + ')';
}
Demo.Employee = function(firstName, lastName, emailAddress, team, title) {
Demo.Employee.initializeBase(this, [firstName, lastName, emailAddress]);
var _team = team;
var _title = title;
this.getTeam = function() {
return _team;
}
this.setTeam = function(team) {
_team = team;
}
this.getTitle = function() {
return _title;
}
this.setTitle = function(title) {
_title = title;
}
}
Demo.Employee.registerClass('Demo.Employee', Demo.Person);
Demo.Employee.prototype.toString = function() {
return Demo.Employee.callBaseMethod(this, 'toString') + '"r"n' + this.getTitle() + '"r"n' + this.getTeam();
}
Type.registerNamespace("Demo");
Demo.Person = function(firstName, lastName, emailAddress) {
var _firstName = firstName;
var _lastName = lastName;
var _emailAddress = emailAddress;
this.getFirstName = function() {
return _firstName;
}
this.getLastName = function() {
return _lastName;
}
this.getEmailAddress = function() {
return _emailAddress;
}
this.setEmailAddress = function(emailAddress) {
_emailAddress = emailAddress;
}
this.getName = function() {
return _firstName + ' ' + _lastName;
}
this.dispose = function() {
alert('bye ' + this.getName());
}
}
Demo.Person.registerClass('Demo.Person', null, Sys.IDisposable);
Demo.Person.prototype.sendMail = function() {
var emailAddress = this.getEmailAddress();
if (emailAddress.indexOf('@') < 0) {
emailAddress = emailAddress + '@example.com';
}
alert('Sending mail to ' + emailAddress + ' ');
}
Demo.Person.prototype.toString = function() {
return this.getName() + ' (' + this.getEmailAddress() + ')';
}
Demo.Employee = function(firstName, lastName, emailAddress, team, title) {
Demo.Employee.initializeBase(this, [firstName, lastName, emailAddress]);
var _team = team;
var _title = title;
this.getTeam = function() {
return _team;
}
this.setTeam = function(team) {
_team = team;
}
this.getTitle = function() {
return _title;
}
this.setTitle = function(title) {
_title = title;
}
}
Demo.Employee.registerClass('Demo.Employee', Demo.Person);
Demo.Employee.prototype.toString = function() {
return Demo.Employee.callBaseMethod(this, 'toString') + '"r"n' + this.getTitle() + '"r"n' + this.getTeam();
}
在ASPX页面中引入该JS文件:
<script type="text/javascript" src="Inheritance.js"></script>
编写一些客户端脚本来进行测试,代码如下所示,每个测试大家可以运行后看一下:
<script type="text/javascript" language="JavaScript">
function GetTestPerson()
{
return new Demo.Person('Jane', 'Doe', 'jane.doe@example.com');
}
function GetTestEmployee()
{
return new Demo.Employee('John', 'Doe', 'john.doe@example.com', 'Platform', 'Programmer');
}
function OnTestNewClick() {
var aPerson = GetTestPerson();
alert(aPerson.getFirstName());
alert(aPerson);
alert(Object.getType(aPerson).getName());
var testPerson = GetTestPerson();
alert(testPerson.getFirstName());
alert(testPerson);
return false;
}
function OnTestDisposeClick() {
var aPerson = GetTestEmployee();
alert(aPerson.getFirstName());
aPerson.dispose();
}
function OnTestPrivatePropertyClick() {
var aPerson = GetTestEmployee();
alert('aPerson._firstName = ' + aPerson._firstName);
alert('aPersona.getFirstName() = ' + aPerson.getFirstName());
return false;
}
function OnTestInstanceMethodClick() {
var aPerson = GetTestEmployee();
aPerson.sendMail('Hello', 'This is a test mail.');
return false;
}
function OnTestOverrideMethodClick() {
var testPerson = GetTestEmployee();
alert(testPerson);
return false;
}
function OnTestInstanceOfClick() {
var aPerson = GetTestEmployee();
if (Demo.Employee.isInstanceOfType(aPerson)) {
alert(aPerson.getName() + ' is an Employee instance."r"nTitle property: ' + aPerson.getTitle());
}
return false;
}
</script>
function GetTestPerson()
{
return new Demo.Person('Jane', 'Doe', 'jane.doe@example.com');
}
function GetTestEmployee()
{
return new Demo.Employee('John', 'Doe', 'john.doe@example.com', 'Platform', 'Programmer');
}
function OnTestNewClick() {
var aPerson = GetTestPerson();
alert(aPerson.getFirstName());
alert(aPerson);
alert(Object.getType(aPerson).getName());
var testPerson = GetTestPerson();
alert(testPerson.getFirstName());
alert(testPerson);
return false;
}
function OnTestDisposeClick() {
var aPerson = GetTestEmployee();
alert(aPerson.getFirstName());
aPerson.dispose();
}
function OnTestPrivatePropertyClick() {
var aPerson = GetTestEmployee();
alert('aPerson._firstName = ' + aPerson._firstName);
alert('aPersona.getFirstName() = ' + aPerson.getFirstName());
return false;
}
function OnTestInstanceMethodClick() {
var aPerson = GetTestEmployee();
aPerson.sendMail('Hello', 'This is a test mail.');
return false;
}
function OnTestOverrideMethodClick() {
var testPerson = GetTestEmployee();
alert(testPerson);
return false;
}
function OnTestInstanceOfClick() {
var aPerson = GetTestEmployee();
if (Demo.Employee.isInstanceOfType(aPerson)) {
alert(aPerson.getName() + ' is an Employee instance."r"nTitle property: ' + aPerson.getTitle());
}
return false;
}
</script>
继承就简单的介绍这么多。