Atlas学习手记(29):JavaScript面向对象的扩展(三):接口Interface

本文介绍Atlas库如何增强JavaScript面向对象能力,重点讲解接口的使用,包括接口注册与实现过程,并提供了一个详细的动物类继承示例。

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

 

主要内容

1.概述

2.完整示例

 

一.概述

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

registerInterface:注册一个接口

registerAbstractClass:注册抽象的基类

使用abstractMethod指定接口中的方法

二.完整示例

看一下Atlas官方网站提供的例子,新建Atlas Web Site,添加一个Inheritance.jsJS文件,其中定义了一个Animal基类和IPet接口,DogCat分别去实现接口,而Tiger类没有实现:

None.gif //  JScript File
None.gif

None.gifType.registerNamespace(
" Demo.Animals " );
None.gif
ExpandedBlockStart.gifContractedBlock.gifDemo.Animals.IPet 
=   function ()  dot.gif {
InBlock.gif
InBlock.gif    
this.returnFriendlyName = Function.abstractMethod;
InBlock.gif
ExpandedBlockEnd.gif}

None.gif
None.gifDemo.Animals.IPet.registerInterface('Demo.Animals.IPet');
None.gif
ExpandedBlockStart.gifContractedBlock.gifDemo.Animals.Animal 
=   function (name)  dot.gif {
InBlock.gif
InBlock.gif    
var _name = name;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
this.returnName = function() dot.gif{
InBlock.gif
InBlock.gif        
return _name;
InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedBlockEnd.gif}

None.gif
None.gifDemo.Animals.Animal.registerAbstractClass('Demo.Animals.Animal');
None.gif
ExpandedBlockStart.gifContractedBlock.gifDemo.Animals.Animal.prototype.toStringCustom 
=   function ()  dot.gif {
InBlock.gif
InBlock.gif    
return this.returnName();
InBlock.gif
ExpandedBlockEnd.gif}

None.gif
None.gifDemo.Animals.Animal.prototype.speak 
=  Function.abstractMethod;
None.gif
ExpandedBlockStart.gifContractedBlock.gifDemo.Animals.Pet 
=   function (name, friendlyName)  dot.gif {
InBlock.gif
InBlock.gif    Demo.Animals.Pet.initializeBase(
this, [name]);
InBlock.gif
InBlock.gif    
var _friendlyName = friendlyName;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
this.returnFriendlyName = function() dot.gif{
InBlock.gif
InBlock.gif        
return _friendlyName;
InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedBlockEnd.gif}

None.gif
None.gifDemo.Animals.Pet.registerAbstractClass('Demo.Animals.Pet', Demo.Animals.Animal, Demo.Animals.IPet);
None.gif
None.gif
ExpandedBlockStart.gifContractedBlock.gifDemo.Animals.Cat 
=   function (friendlyName)  dot.gif {
InBlock.gif
InBlock.gif    Demo.Animals.Cat.initializeBase(
this, ['Cat', friendlyName]);
InBlock.gif
ExpandedBlockEnd.gif}

None.gif
None.gifDemo.Animals.Cat.registerClass('Demo.Animals.Cat', Demo.Animals.Pet);
None.gif
ExpandedBlockStart.gifContractedBlock.gifDemo.Animals.Cat.prototype.speak 
=   function ()  dot.gif {
InBlock.gif
InBlock.gif    alert('meow');
InBlock.gif
ExpandedBlockEnd.gif}

None.gif
ExpandedBlockStart.gifContractedBlock.gifDemo.Animals.Cat.prototype.toStringCustom 
=   function ()  dot.gif {
InBlock.gif
InBlock.gif    
return 'Pet ' + Demo.Animals.Cat.callBaseMethod(this, 'toStringCustom');
InBlock.gif
ExpandedBlockEnd.gif}

None.gif
ExpandedBlockStart.gifContractedBlock.gifDemo.Animals.Felix 
=   function ()  dot.gif {
InBlock.gif
InBlock.gif    Demo.Animals.Felix.initializeBase(
this, ['Felix']);
InBlock.gif
ExpandedBlockEnd.gif}

None.gif
None.gifDemo.Animals.Felix.registerClass('Demo.Animals.Felix', Demo.Animals.Cat);
None.gif
ExpandedBlockStart.gifContractedBlock.gifDemo.Animals.Felix.prototype.toStringCustom 
=   function ()  dot.gif {
InBlock.gif
InBlock.gif    
return Demo.Animals.Felix.callBaseMethod(this, 'toStringCustom') + ' dot.gif its Felix!';
InBlock.gif
ExpandedBlockEnd.gif}

None.gif
ExpandedBlockStart.gifContractedBlock.gifDemo.Animals.Dog 
=   function (friendlyName)  dot.gif {
InBlock.gif
InBlock.gif    Demo.Animals.Dog.initializeBase(
this, ['Dog', friendlyName]);
InBlock.gif
ExpandedBlockEnd.gif}

None.gif
None.gifDemo.Animals.Dog.registerClass('Demo.Animals.Dog', Demo.Animals.Pet);
None.gif
ExpandedBlockStart.gifContractedBlock.gifDemo.Animals.Dog.prototype.speak 
=   function ()  dot.gif {
InBlock.gif
InBlock.gif    alert('woof');
InBlock.gif
ExpandedBlockEnd.gif}

None.gif
ExpandedBlockStart.gifContractedBlock.gifDemo.Animals.Tiger 
=   function ()  dot.gif {
InBlock.gif
InBlock.gif    Demo.Animals.Tiger.initializeBase(
this, ['Tiger']);
InBlock.gif
ExpandedBlockEnd.gif}

None.gif
None.gifDemo.Animals.Tiger.registerClass('Demo.Animals.Tiger', Demo.Animals.Animal);
None.gif
ExpandedBlockStart.gifContractedBlock.gifDemo.Animals.Tiger.prototype.speak 
=   function ()  dot.gif {
InBlock.gif
InBlock.gif    alert('grrr');
InBlock.gif
ExpandedBlockEnd.gif}

ASPX页面中引入该JS文件:

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

编写脚本,做一些简单的测试:

ExpandedBlockStart.gif ContractedBlock.gif < script  type ="text/javascript"  language ="JavaScript" > dot.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
function OnTestNewClick() dot.gif{
InBlock.gif
InBlock.gif        
var cat = new Demo.Animals.Cat('Kitty');
InBlock.gif
InBlock.gif        alert(cat.returnName());
InBlock.gif
InBlock.gif        cat.speak();
InBlock.gif
InBlock.gif        
return false;
InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
function OnTestImplementsClick() dot.gif{
InBlock.gif
InBlock.gif        
var cat = new Demo.Animals.Cat('Kitty');
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
if (Demo.Animals.IPet.isImplementedBy(cat)) dot.gif{
InBlock.gif
InBlock.gif            alert('Cat implements IPet');
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
else dot.gif{
InBlock.gif
InBlock.gif            alert('Cat does not implement IPet');
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
var tiger = new Demo.Animals.Tiger();
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
if (Demo.Animals.IPet.isImplementedBy(tiger)) dot.gif{
InBlock.gif
InBlock.gif            alert('Tiger implements IPet');
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
else dot.gif{
InBlock.gif
InBlock.gif            alert('Tiger does not implement IPet');
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
return false;
InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
function OnTestInterfaceMethodClick() dot.gif{
InBlock.gif
InBlock.gif        
var cat = new Demo.Animals.Cat('Kitty');
InBlock.gif
InBlock.gif        ProcessAnimal(cat);
InBlock.gif
InBlock.gif        
var tiger = new Demo.Animals.Tiger();
InBlock.gif
InBlock.gif        ProcessAnimal(tiger);
InBlock.gif
InBlock.gif        
var dog = new Demo.Animals.Dog('Joe');
InBlock.gif
InBlock.gif        ProcessAnimal(dog);
InBlock.gif
InBlock.gif        
var g = new Demo.Animals.Felix();
InBlock.gif
InBlock.gif        ProcessAnimal(g);
InBlock.gif
InBlock.gif        
return false;
InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
function ProcessAnimal(animal) dot.gif{
InBlock.gif
InBlock.gif        alert('Current animal ' 
+ animal.returnName());
InBlock.gif
InBlock.gif        alert(animal.toStringCustom());
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
if (Demo.Animals.IPet.isImplementedBy(animal)) dot.gif{
InBlock.gif
InBlock.gif            alert(animal.returnName() 
+ ' implements IPet; friendlyName: ' + animal.returnFriendlyName());
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
if (Demo.Animals.Felix.isInstanceOfType(animal)) dot.gif{
InBlock.gif
InBlock.gif            alert('No ordinary catdot.gif its Felix
!');
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif
None.gif
</ script >

关于接口的介绍就到这里了。 

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值