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

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

主要内容

1.概述

2.完整示例

一.概述

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

registerInterface:注册一个接口

registerAbstractClass:注册抽象的基类

使用abstractMethod指定接口中的方法

二.完整示例

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

// JScript File

Type.registerNamespace(
"Demo.Animals");

Demo.Animals.IPet 
= function() {

    
this.returnFriendlyName = Function.abstractMethod;

}


Demo.Animals.IPet.registerInterface('Demo.Animals.IPet');

Demo.Animals.Animal 
= function(name) {

    
var _name = name;

    
this.returnName = function() {

        
return _name;

    }


}


Demo.Animals.Animal.registerAbstractClass('Demo.Animals.Animal');

Demo.Animals.Animal.prototype.toStringCustom 
= function() {

    
return this.returnName();

}


Demo.Animals.Animal.prototype.speak 
= Function.abstractMethod;

Demo.Animals.Pet 
= function(name, friendlyName) {

    Demo.Animals.Pet.initializeBase(
this, [name]);

    
var _friendlyName = friendlyName;

    
this.returnFriendlyName = function() {

        
return _friendlyName;

    }


}


Demo.Animals.Pet.registerAbstractClass('Demo.Animals.Pet', Demo.Animals.Animal, Demo.Animals.IPet);


Demo.Animals.Cat 
= function(friendlyName) {

    Demo.Animals.Cat.initializeBase(
this, ['Cat', friendlyName]);

}


Demo.Animals.Cat.registerClass('Demo.Animals.Cat', Demo.Animals.Pet);

Demo.Animals.Cat.prototype.speak 
= function() {

    alert('meow');

}


Demo.Animals.Cat.prototype.toStringCustom 
= function() {

    
return 'Pet ' + Demo.Animals.Cat.callBaseMethod(this, 'toStringCustom');

}


Demo.Animals.Felix 
= function() {

    Demo.Animals.Felix.initializeBase(
this, ['Felix']);

}


Demo.Animals.Felix.registerClass('Demo.Animals.Felix', Demo.Animals.Cat);

Demo.Animals.Felix.prototype.toStringCustom 
= function() {

    
return Demo.Animals.Felix.callBaseMethod(this, 'toStringCustom') + '  its Felix!';

}


Demo.Animals.Dog 
= function(friendlyName) {

    Demo.Animals.Dog.initializeBase(
this, ['Dog', friendlyName]);

}


Demo.Animals.Dog.registerClass('Demo.Animals.Dog', Demo.Animals.Pet);

Demo.Animals.Dog.prototype.speak 
= function() {

    alert('woof');

}


Demo.Animals.Tiger 
= function() {

    Demo.Animals.Tiger.initializeBase(
this, ['Tiger']);

}


Demo.Animals.Tiger.registerClass('Demo.Animals.Tiger', Demo.Animals.Animal);

Demo.Animals.Tiger.prototype.speak 
= function() {

    alert('grrr');

}

在ASPX页面中引入该JS文件:

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

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

<script type="text/javascript" language="JavaScript">

    
function OnTestNewClick() {

        
var cat = new Demo.Animals.Cat('Kitty');

        alert(cat.returnName());

        cat.speak();

        
return false;

    }


    
function OnTestImplementsClick() {

        
var cat = new Demo.Animals.Cat('Kitty');

        
if (Demo.Animals.IPet.isImplementedBy(cat)) {

            alert('Cat implements IPet');

        }


        
else {

            alert('Cat does not implement IPet');

        }


        
var tiger = new Demo.Animals.Tiger();

        
if (Demo.Animals.IPet.isImplementedBy(tiger)) {

            alert('Tiger implements IPet');

        }


        
else {

            alert('Tiger does not implement IPet');

        }


        
return false;

    }


    
function OnTestInterfaceMethodClick() {

        
var cat = new Demo.Animals.Cat('Kitty');

        ProcessAnimal(cat);

        
var tiger = new Demo.Animals.Tiger();

        ProcessAnimal(tiger);

        
var dog = new Demo.Animals.Dog('Joe');

        ProcessAnimal(dog);

        
var g = new Demo.Animals.Felix();

        ProcessAnimal(g);

        
return false;

    }


    
function ProcessAnimal(animal) {

        alert('Current animal ' 
+ animal.returnName());

        alert(animal.toStringCustom());

        
if (Demo.Animals.IPet.isImplementedBy(animal)) {

            alert(animal.returnName() 
+ ' implements IPet; friendlyName: ' + animal.returnFriendlyName());

        }


        
if (Demo.Animals.Felix.isInstanceOfType(animal)) {

            alert('No ordinary cat its Felix
!');

        }

    }


</script>

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

完整示例下载

posted on   秋天  阅读(263)  评论(0编辑  收藏  举报

编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构

导航

< 2008年3月 >
24 25 26 27 28 29 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示