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

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

 

主要内容

1.概述

2.完整示例

 

一.概述

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

registerInterface:注册一个接口

registerAbstractClass:注册抽象的基类

使用abstractMethod指定接口中的方法

二.完整示例

看一下Atlas官方网站提供的例子,新建Atlas Web Site,添加一个Inheritance.jsJS文件,其中定义了一个Animal基类和IPet接口,DogCat分别去实现接口,而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>

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

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

posted @   TerryLee  阅读(5095)  评论(2编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
点击右上角即可分享
微信分享提示