[转] Atlas与面向对象的Javascript

英文链接: http://atlas.asp.net/docs/atlas/doc/javascript/default.aspx

按我的理解,面向对象基本的特性无非六个字:封装、继承和多态。研究了Atlas的文档之后,发现它的Javascript脚本库把这几个特性都实现了。Ok, Let go......

先看看封装

Atlas提供了命名空间Namespace的功能来隔离命名冲突,而且在类中可以定义私有变量,达到的封装的目的。可惜的是类的方法好像只能是公共的。

以下是示例代码(所有的示例代码来自于原文链接)

 

Type.registerNamespace("Demo");

Demo.Person = function(firstName, lastName, emailAddress) {
    var _firstName = firstName;
    var _lastName = lastName;
    var _emailAddress = emailAddress;
   
    this.getFirstName = function() {
        return _firstName;
    }
   
    this.getLastName = function() {
        return _lastName;
    }
   
    this.getEmailAddress = function() {
        return _emailAddress;
    }
   
    this.setEmailAddress = function(emailAddress) {
        _emailAddress = emailAddress;
    }
   
    this.getName = function() {
        return _firstName + ' ' + _lastName;
    }

    this.dispose = function() {
        alert('bye ' + this.getName());
    }
}
Demo.Person.registerClass('Demo.Person', null, Sys.IDisposable);
定义了以上的Demo.Person类之后,就可以在页面中的Javascript代码中按OO的方法使用了。

 

        var testPerson = new Demo.Person('John', 'Smith', 'john.smith@example.com');
        alert(testPerson.getFirstName() + " " + testPerson.getLastName() );

然后来看看继承

 

Demo.Person.prototype.sendMail = function() {
    var emailAddress = this.getEmailAddress();

    if (emailAddress.indexOf('@') < 0) {
        emailAddress = emailAddress + '@example.com';
    }
    alert('Sending mail to ' + emailAddress + ' ');
}

Demo.Person.prototype.toString = function() {
    return this.getName() + ' (' + this.getEmailAddress() + ')';
}

Demo.Employee = function(firstName, lastName, emailAddress, team, title) {
    Demo.Employee.initializeBase(this, [firstName, lastName, emailAddress]);
   
    var _team = team;
    var _title = title;
   
    this.getTeam = function() {
        return _team;
    }
    this.setTeam = function(team) {
        _team = team;
    }
   
    this.getTitle = function() {
        return _title;
    }
    this.setTitle = function(title) {
        _title = title;
    }
}
Demo.Employee.registerClass('Demo.Employee', Demo.Person);

Demo.Employee.prototype.toString = function() {
    return Demo.Employee.callBaseMethod(this, 'toString') + '\r\n' + this.getTitle() + '\r\n' + this.getTeam();
}

简单地说,就是用prototype定义虚函数。使用registerCalss定义基类,构造函数调用initializeBase

最后,我们来看看多态

其实上面的代码toString函数已经实现了继承的多态。我们再来看看接口实现的多态:

 

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');
}
首先,用registerInterface定义一个接口

然后,在registerClass的时候,第三个参数指定类实现了哪个接口

Atlas还支持抽象类的定义(registerAbstractClass)

确实算是比较完备了。

FROM: http://www.cnblogs.com/didasoft/archive/2006/03/30/362863.html

原文地址:https://www.cnblogs.com/imbin/p/1116753.html