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');
}
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>
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
支持TerryLee的创业产品Worktile
Worktile,新一代简单好用、体验极致的团队协同、项目管理工具,让你和你的团队随时随地一起工作。完全免费,现在就去了解一下吧。
https://worktile.com
Worktile,新一代简单好用、体验极致的团队协同、项目管理工具,让你和你的团队随时随地一起工作。完全免费,现在就去了解一下吧。
https://worktile.com