Walter She's Blog

flash,flex ,javascript原创教程,web设计,Flash RIA,其它与之相关的内容.

« 介绍一个源文件下载网址保存数据到磁盘(翻译) »

在Flash类中实现多个构造函数

此教程摘抄于 Essential Actionscript.. 2.0 一书
作者:Colin Moock .................................

class Box {

  public var width:Number;

  public var height:Number;


  public function Box (a1:Object, a2:Object) {

    if (arguments.length == 0) {

      boxNoArgs( );

    } else if (typeof a1 == "string") {

      boxString(a1);

    } else if (typeof a1 == "number" && typeof a2 == "number") {

      boxNumberNumber(a1, a2);

    } else {

      trace("Unexpected number of arguments passed to Box constructor.");

    }

  }



  private function boxNoArgs ( ):Void {

    if (arguments.caller != Box) {

      return;

    }

    width  = 1;

    height = 1;

  }


  private function boxString (size):Void {
    if (arguments.caller != Box) {

      return;

    }

    if (size == "large") {

      width  = 100;

      height = 100;

    } else if (size == "small") {

      width  = 10;

      height = 10;

    } else {

      trace("Invalid box size specified");

    }

  }



  private function boxNumberNumber (w, h):Void {

    if (arguments.caller != Box) {

      return;

    }

    width  = w;

    height = h;

  }

}


// 用法:

var b1:Box = new Box( );

trace(b1.width); // 输出: 1



var b2:Box = new Box("large");

trace(b2.width); // 输出: 100



var b3:Box = new Box(25, 35);

trace(b3.width); // 输出: 25

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

日历

最新评论及回复

最近发表