zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    1104 Sum of Number Segments (20 分)(数学问题)
    1092 To Buy or Not to Buy (20 分)(hash散列)
    1082 Read Number in Chinese (25 分)(字符串处理)【背】
    1105 Spiral Matrix (25 分)(模拟)
    初识网络安全及搭建网站(内网)
    HTML5开发者需要了解的技巧和工具汇总(转)
    native+web开发模式之web前端经验分享
    移动平台3G手机网站前端开发布局技巧汇总(转)
    Asp.net 中图片存储数据库以及页面读取显示通用方法详解附源码下载
    使用H3Viewer来查看VS2010的帮助文档
  • 原文地址:https://www.cnblogs.com/Terrylee/p/Atlas_Javascript_Interface.html
Copyright © 2011-2022 走看看