作者: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