¿ Overloaded Methods ?

8 posts

Flag Post

I am doing some basic inheritance stuff: classX, classY extends classX. In my classY I have a constructor


public function classY ( param1:Number ){ super.classX()…}


That works fine. but in my classY I also want to have another constructor as the following:


public function classY ( param1:Number, param2:Number ) {…}


But it complains about duplicate names. I know there cant be the same name as a var and a function, but does AS3.0 not support overloading a function? I tried override keyword but that didn’t work.

 
Flag Post

You cant have duplicate constructors, there’s no real reason to anyway. If you want param2 to be optional, give it a default value, use the … (rest) parameter, or else use one of the other options available.

public function classY ( param1:Number, param2:Number = -1 ) {...} 
public function classY ( param1:Number, ...args ) {...} 
 
Flag Post

Overloading a function generally has to do with the parameters of the single function. So, if you check the


arguments.length

property, you can see how many arguments have been passed into the function and determine your course of action.

 
Flag Post

yes those optional params will work well thanks.



public function classA ( name:String= “default_Name” ){ this.myName /* just a class var */ = name; …}


will that work as a default constructor because in classZ i call this:


classZ ( obj:classA, val:int ) { … }

The point is to pass in an already created classA object to classZ
because i get error 1203: No default constructor found in base class stick:classA

 
Flag Post

Cool, glad it helped.

@Indie: In AS3, the … (rest) parameter is recommended over the arguments object, except in special cases like using argument.callee. If you use the …rest parameter you wont have access to the argument object anyway, so its good to choose one or the other based on your needs.

 
Flag Post

In AS3, the … (rest) parameter is recommended over the arguments object, except in special cases like using argument.callee.

D’oh.
I’m ordering my first AS3 book post-haste, I can’t live in the darkness any longer :0
All this time, I’ve been trying to hack this stuff out in AS2. hehe

 
Flag Post

Knowing you, I think you’ll love it ;)

 
Flag Post

“D’oh. I’m ordering my first AS3 book post-haste, I can’t live in the darkness any longer :0 All this time, I’ve been trying to hack this stuff out in AS2. hehe”
Indie, Essential ActioinScript 3.0 by Colin Moock. Good Choice.