在Javascript中并没有空间、类、接口这些概念,Atlas对这些东西实现封装了,增强了JavaScript面向对象方面的能力,本文看一下如何使用接口。
主要内容
1.概述
2.完整示例
在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
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Type.registerNamespace("Demo.Animals");
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
Demo.Animals.IPet = function()
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this.returnFriendlyName = Function.abstractMethod;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Demo.Animals.IPet.registerInterface('Demo.Animals.IPet');
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
Demo.Animals.Animal = function(name)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
var _name = name;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
this.returnName = function()
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return _name;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Demo.Animals.Animal.registerAbstractClass('Demo.Animals.Animal');
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
Demo.Animals.Animal.prototype.toStringCustom = function()
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return this.returnName();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Demo.Animals.Animal.prototype.speak = Function.abstractMethod;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
Demo.Animals.Pet = function(name, friendlyName)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Demo.Animals.Pet.initializeBase(this, [name]);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
var _friendlyName = friendlyName;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
this.returnFriendlyName = function()
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return _friendlyName;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Demo.Animals.Pet.registerAbstractClass('Demo.Animals.Pet', Demo.Animals.Animal, Demo.Animals.IPet);
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
Demo.Animals.Cat = function(friendlyName)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Demo.Animals.Cat.initializeBase(this, ['Cat', friendlyName]);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Demo.Animals.Cat.registerClass('Demo.Animals.Cat', Demo.Animals.Pet);
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
Demo.Animals.Cat.prototype.speak = function()
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
alert('meow');
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
Demo.Animals.Cat.prototype.toStringCustom = function()
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return 'Pet ' + Demo.Animals.Cat.callBaseMethod(this, 'toStringCustom');
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
Demo.Animals.Felix = function()
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Demo.Animals.Felix.initializeBase(this, ['Felix']);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Demo.Animals.Felix.registerClass('Demo.Animals.Felix', Demo.Animals.Cat);
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
Demo.Animals.Felix.prototype.toStringCustom = function()
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return Demo.Animals.Felix.callBaseMethod(this, 'toStringCustom') + '
its Felix!';
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
Demo.Animals.Dog = function(friendlyName)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Demo.Animals.Dog.initializeBase(this, ['Dog', friendlyName]);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Demo.Animals.Dog.registerClass('Demo.Animals.Dog', Demo.Animals.Pet);
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
Demo.Animals.Dog.prototype.speak = function()
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
alert('woof');
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
Demo.Animals.Tiger = function()
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Demo.Animals.Tiger.initializeBase(this, ['Tiger']);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Demo.Animals.Tiger.registerClass('Demo.Animals.Tiger', Demo.Animals.Animal);
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
Demo.Animals.Tiger.prototype.speak = function()
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
alert('grrr');
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
在ASPX页面中引入该JS文件:
<script type="text/javascript" src="Interface.js"></script>
编写脚本,做一些简单的测试:
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<script type="text/javascript" language="JavaScript">![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
function OnTestNewClick()
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
var cat = new Demo.Animals.Cat('Kitty');
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
alert(cat.returnName());
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
cat.speak();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return false;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
function OnTestImplementsClick()
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
var cat = new Demo.Animals.Cat('Kitty');
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (Demo.Animals.IPet.isImplementedBy(cat))
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
alert('Cat implements IPet');
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
else
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
alert('Cat does not implement IPet');
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
var tiger = new Demo.Animals.Tiger();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (Demo.Animals.IPet.isImplementedBy(tiger))
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
alert('Tiger implements IPet');
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
else
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
alert('Tiger does not implement IPet');
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return false;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
function OnTestInterfaceMethodClick()
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
var cat = new Demo.Animals.Cat('Kitty');
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
ProcessAnimal(cat);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
var tiger = new Demo.Animals.Tiger();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
ProcessAnimal(tiger);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
var dog = new Demo.Animals.Dog('Joe');
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
ProcessAnimal(dog);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
var g = new Demo.Animals.Felix();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
ProcessAnimal(g);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return false;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
function ProcessAnimal(animal)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
alert('Current animal ' + animal.returnName());
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
alert(animal.toStringCustom());
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (Demo.Animals.IPet.isImplementedBy(animal))
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
alert(animal.returnName() + ' implements IPet; friendlyName: ' + animal.returnFriendlyName());
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (Demo.Animals.Felix.isInstanceOfType(animal))
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
alert('No ordinary cat
its Felix!');
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
</script>
关于接口的介绍就到这里了。
完整示例下载:https://files.cnblogs.com/Terrylee/AtlasInterfaceDemo.rar