点击这里给我发消息 点击这里给我发消息

Atlas学习手记(28):JavaScript面向对象的扩展(二):继承Inh

添加时间:2013-12-6
    相关阅读: ASP 页面 JavaScript 网站

  主要内容

  1.概述

  2.完整示例

  一.概述

  在Javascript中并没有空间、类、接口这些概念,Atlas对这些东西实现封装了,增强了JavaScript面向对象方面的能力,本文看一下如何使用继承。简单定义一个可被继承的基类,在注册类时指定类名就可以了:

BaseClass = function()
{
  // ……
}
BaseClass.registerClass("BaseClass");

  定义一个继承类,先要调用父类的构造器,除了传递本身之外还可以传递一些参数,注册类时需要指定继承自哪个类:

DerivedClass = function()
{
    // ……
    DerivedClass.intializeBase(this,arguments);
}
DerivedClass.registerClass("DerivedClass","BaseClass");

  二.完整示例

  看一下Atlas官方网站提供的例子,新建Atlas Web Site,添加一个Inheritance.js的JS文件,定义Person 和Employee两个类 ,并且让Employee继承于Person,Employee覆写父类中的toString方法:

// JScript File
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.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') + 'rn' + this.getTitle() + 'rn' + this.getTeam();
}

在ASPX页面中引入该JS文件:

<script type="text/javascript" src="Inheritance.js"></script>

  编写一些客户端脚本来进行测试,代码如下所示,每个测试大家可以运行后看一下:

<script type="text/javascript" language="JavaScript">
  function GetTestPerson()
  {
    return new Demo.Person('Jane', 'Doe', 'jane.doe@example.com');
  }
  function GetTestEmployee()
  {
    return new Demo.Employee('John', 'Doe', 'john.doe@example.com', 'Platform', 'Programmer');
  }
  function OnTestNewClick() {
    var aPerson = GetTestPerson();
    alert(aPerson.getFirstName());
    alert(aPerson);
    alert(Object.getType(aPerson).getName());
    var testPerson = GetTestPerson();
    alert(testPerson.getFirstName());
    alert(testPerson);
    return false;
  }
  function OnTestDisposeClick() {
    var aPerson = GetTestEmployee();
    alert(aPerson.getFirstName());
    aPerson.dispose();
  }
  function OnTestPrivatePropertyClick() {
    var aPerson = GetTestEmployee();
    alert('aPerson._firstName = ' + aPerson._firstName);
    alert('aPersona.getFirstName() = ' + aPerson.getFirstName());
    return false;
  }
  function OnTestInstanceMethodClick() {
    var aPerson = GetTestEmployee();
    aPerson.sendMail('Hello', 'This is a test mail.');
    return false;
  }
  function OnTestOverrideMethodClick() {
    var testPerson = GetTestEmployee();
    alert(testPerson);
    return false;
  }
  function OnTestInstanceOfClick() {
    var aPerson = GetTestEmployee();
    if (Demo.Employee.isInstanceOfType(aPerson)) {
      alert(aPerson.getName() + ' is an Employee instance.rnTitle property: ' + aPerson.getTitle());
    }
    return false;
  }
</script>


  继承就简单的介绍这么多

本文作者:
咨询热线:020-85648757 85648755 85648616 0755-27912581 客服:020-85648756 0755-27912581 业务传真:020-32579052
广州市网景网络科技有限公司 Copyright◎2003-2008 Veelink.com. All Rights Reserved.
广州商务地址:广东省广州市黄埔大道中203号(海景园区)海景花园C栋501室
= 深圳商务地址:深圳市宝源路华丰宝源大厦606
研发中心:广东广州市天河软件园海景园区 粤ICP备05103322号 工商注册