开发者

ActionScript-3 cannot have multiple constructors?

开发者 https://www.devze.com 2023-01-13 11:02 出处:网络
I have public fun开发者_JS百科ction Shard() { } public function Shard(x:Number, y:Number, vx:Number, vy:Number, rotation:Number, spin:Number)

I have

    public fun开发者_JS百科ction Shard() {
    }

    public function Shard(x:Number, y:Number, vx:Number, vy:Number, rotation:Number, spin:Number)
    {
      ...
    }

And I got on the second constructor:

Multiple constructor definitions found. Constructor may not be defined in code.

So, ActionScript-3 cannot have multiple constructors?


No you can't have multiple contructor neither the same function with different signature, but you can use default arguments :

public function Shard(x:Number=NaN, y:Number=NaN) {
  //...
}

then you can call new Shard() or new Shard(100) or new Shard(100, 200)

or you can also use variable arguments:

public function Shard(...args){
 if (args.length==0) {
   //...
 } else {
  //...
  var firstArg:Object=args[0];
  //...
 }
}


It's even broader than that. AS3 has no support at all for function overloading.

You can simulate the appearance of multiple constructors with parameter defaults and/or dynamically dispatching the parameters to various init methods. Alternatively, custom object factories or static creation methods could help.

0

精彩评论

暂无评论...
验证码 换一张
取 消