Atlas学习手记(28):JavaScript面向对象的扩展(二):继承Inheritance

本文通过示例展示了如何在Atlas框架下实现JavaScript的面向对象编程,特别是继承的使用。通过定义Person和Employee类,演示了如何创建基类、继承类及方法重写。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Javascript中并没有空间、类、接口这些概念,Atlas对这些东西实现封装了,增强了JavaScript面向对象方面的能力,本文看一下如何使用继承。

 

主要内容

1.概述

2.完整示例

 

一.概述

Javascript中并没有空间、类、接口这些概念,Atlas对这些东西实现封装了,增强了JavaScript面向对象方面的能力,本文看一下如何使用继承。简单定义一个可被继承的基类,在注册类时指定类名就可以了:

None.gif BaseClass  =   function ()
None.gif
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
InBlock.gif    
// ……
InBlock.gif

ExpandedBlockEnd.gif}

None.gif
None.gifBaseClass.registerClass(
" BaseClass " );

定义一个继承类,先要调用父类的构造器,除了传递本身之外还可以传递一些参数,注册类时需要指定继承自哪个类:

None.gif DerivedClass  =   function ()
None.gif
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif       
// ……
InBlock.gif

InBlock.gif       DerivedClass.intializeBase(
this,arguments); 
InBlock.gif
ExpandedBlockEnd.gif}

None.gif
None.gifDerivedClass.registerClass(
" DerivedClass " , " BaseClass " );

二.完整示例

看一下Atlas官方网站提供的例子,新建Atlas Web Site,添加一个Inheritance.jsJS文件,定义Person Employee两个类 ,并且让Employee继承于PersonEmployee覆写父类中的toString方法:

None.gif //  JScript File
None.gif

None.gifType.registerNamespace(
" Demo " );
None.gif
ExpandedBlockStart.gifContractedBlock.gifDemo.Person 
=   function (firstName, lastName, emailAddress)  dot.gif {
InBlock.gif
InBlock.gif    
var _firstName = firstName;
InBlock.gif
InBlock.gif    
var _lastName = lastName;
InBlock.gif
InBlock.gif    
var _emailAddress = emailAddress;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
this.getFirstName = function() dot.gif{
InBlock.gif
InBlock.gif        
return _firstName;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
this.getLastName = function() dot.gif{
InBlock.gif
InBlock.gif        
return _lastName;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
this.getEmailAddress = function() dot.gif{
InBlock.gif
InBlock.gif        
return _emailAddress;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
this.setEmailAddress = function(emailAddress) dot.gif{
InBlock.gif
InBlock.gif        _emailAddress 
= emailAddress;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
this.getName = function() dot.gif{
InBlock.gif
InBlock.gif        
return _firstName + ' ' + _lastName;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
this.dispose = function() dot.gif{
InBlock.gif
InBlock.gif        alert('bye ' 
+ this.getName());
InBlock.gif
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
None.gifDemo.Person.registerClass('Demo.Person', 
null , Sys.IDisposable);
None.gif
ExpandedBlockStart.gifContractedBlock.gifDemo.Person.prototype.sendMail 
=   function ()  dot.gif {
InBlock.gif
InBlock.gif    
var emailAddress = this.getEmailAddress();
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
if (emailAddress.indexOf('@') < 0dot.gif{
InBlock.gif
InBlock.gif        emailAddress 
= emailAddress + '@example.com';
InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    alert('Sending mail to ' 
+ emailAddress + ' dot.gif');
InBlock.gif
ExpandedBlockEnd.gif}

None.gif
ExpandedBlockStart.gifContractedBlock.gifDemo.Person.prototype.toString 
=   function ()  dot.gif {
InBlock.gif
InBlock.gif    
return this.getName() + ' (' + this.getEmailAddress() + ')';
InBlock.gif
ExpandedBlockEnd.gif}

None.gif
ExpandedBlockStart.gifContractedBlock.gifDemo.Employee 
=   function (firstName, lastName, emailAddress, team, title)  dot.gif {
InBlock.gif
InBlock.gif    Demo.Employee.initializeBase(
this, [firstName, lastName, emailAddress]);
InBlock.gif
InBlock.gif    
var _team = team;
InBlock.gif
InBlock.gif    
var _title = title;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
this.getTeam = function() dot.gif{
InBlock.gif
InBlock.gif        
return _team;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
this.setTeam = function(team) dot.gif{
InBlock.gif
InBlock.gif        _team 
= team;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
this.getTitle = function() dot.gif{
InBlock.gif
InBlock.gif        
return _title;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
this.setTitle = function(title) dot.gif{
InBlock.gif
InBlock.gif        _title 
= title;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedBlockEnd.gif}

None.gif
None.gifDemo.Employee.registerClass('Demo.Employee', Demo.Person);
None.gif
ExpandedBlockStart.gifContractedBlock.gifDemo.Employee.prototype.toString 
=   function ()  dot.gif {
InBlock.gif
InBlock.gif    
return Demo.Employee.callBaseMethod(this, 'toString') + '\r\n' + this.getTitle() + '\r\n' + this.getTeam();
InBlock.gif
ExpandedBlockEnd.gif}

ASPX页面中引入该JS文件:

None.gif < script  type ="text/javascript"  src ="Inheritance.js" ></ script >

编写一些客户端脚本来进行测试,代码如下所示,每个测试大家可以运行后看一下:

ExpandedBlockStart.gif ContractedBlock.gif < script  type ="text/javascript"  language ="JavaScript" > dot.gif
InBlock.gif
InBlock.gif    
function GetTestPerson() 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
return new Demo.Person('Jane', 'Doe', 'jane.doe@example.com');
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
function GetTestEmployee() 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
return new Demo.Employee('John', 'Doe', 'john.doe@example.com', 'Platform', 'Programmer');
InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
function OnTestNewClick() dot.gif{
InBlock.gif
InBlock.gif        
var aPerson = GetTestPerson();
InBlock.gif
InBlock.gif        alert(aPerson.getFirstName());
InBlock.gif
InBlock.gif        alert(aPerson);
InBlock.gif
InBlock.gif        alert(Object.getType(aPerson).getName());
InBlock.gif
InBlock.gif        
var testPerson = GetTestPerson();
InBlock.gif
InBlock.gif        alert(testPerson.getFirstName());
InBlock.gif
InBlock.gif        alert(testPerson);
InBlock.gif
InBlock.gif        
return false;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
function OnTestDisposeClick() dot.gif{
InBlock.gif
InBlock.gif        
var aPerson = GetTestEmployee();
InBlock.gif
InBlock.gif        alert(aPerson.getFirstName());
InBlock.gif
InBlock.gif        aPerson.dispose();
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
function OnTestPrivatePropertyClick() dot.gif{
InBlock.gif
InBlock.gif        
var aPerson = GetTestEmployee();
InBlock.gif
InBlock.gif        alert('aPerson._firstName 
= ' + aPerson._firstName);
InBlock.gif
InBlock.gif        alert('aPersona.getFirstName() 
= ' + aPerson.getFirstName());
InBlock.gif
InBlock.gif        
return false;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
function OnTestInstanceMethodClick() dot.gif{
InBlock.gif
InBlock.gif        
var aPerson = GetTestEmployee();
InBlock.gif
InBlock.gif        aPerson.sendMail('Hello', 'This is a test mail.');
InBlock.gif
InBlock.gif        
return false;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
function OnTestOverrideMethodClick() dot.gif{
InBlock.gif
InBlock.gif        
var testPerson = GetTestEmployee();
InBlock.gif
InBlock.gif        alert(testPerson);
InBlock.gif
InBlock.gif        
return false;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
function OnTestInstanceOfClick() dot.gif{
InBlock.gif
InBlock.gif        
var aPerson = GetTestEmployee();
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
if (Demo.Employee.isInstanceOfType(aPerson)) dot.gif{
InBlock.gif
InBlock.gif            alert(aPerson.getName() 
+ ' is an Employee instance.\r\nTitle property: ' + aPerson.getTitle());
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
return false;
ExpandedBlockEnd.gif    }

None.gif
</ script >

继承就简单的介绍这么多。 

完整示例下载:http://files.cnblogs.com/Terrylee/AtlasInheritanceDemo.rar

转载于:https://www.cnblogs.com/Terrylee/archive/2006/09/17/Atlas_Javascript_Inheritance.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值