1 Replies - 4716 Views - Last Post: 31 May 2010 - 04:05 PM

#1 depricated  Icon User is online

  • Derpicorn

Reputation: 824
  • View blog
  • Posts: 2,671
  • Joined: 13-September 08

Multiple Same-Name Functions/Constructors in AS3

Posted 20 May 2010 - 02:59 AM

Is it possible to have multiple constructors?

I keep getting an error when I try to make more than one. Is there a trick to it, or am I just going to have to use a lot of accessor functions to build new objects?

I'm trying it the same way I would in Java or C:
public function Class()
{
}

public function Class(x:int, y:int)
{
}

public function Class(a:Array)
{
}


Come to think of it I get the same thing when I create functions this way as well, such as

public function move(x:int, y:int):void
{
}

public function move(a:array):void
{
}



Or when I override something like
override public function move():void
{
}


where the overridden function would look something like
public function move(x:int, y:int):void
{
}



Do I just need to use unique names for all functions and only get one constructor?

Is This A Good Question/Topic? 0
  • +

Replies To: Multiple Same-Name Functions/Constructors in AS3

#2 AdaHacker  Icon User is offline

  • Resident Curmudgeon

Reputation: 452
  • View blog
  • Posts: 811
  • Joined: 17-June 08

Re: Multiple Same-Name Functions/Constructors in AS3

Posted 31 May 2010 - 04:05 PM

Nope, there's no method overloading in ActionScript 3. You only get one constructor and very method needs a unique name. However, you can fake it with things like variable parameters and wildcard types and checking types in the body. For instance:
public function move(x:*, y:int=0):void {
    if (x is Array) {
        // Do stuff for array version
    } else if (x is int) {
        // Do stuff for int version
    }
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1