Friday, September 25, 2009

Helper Classes in ActionScript

I've had a few people ask me recently if you can have multiple classes defined in the same file in ActionScript. This is something that Java developers are probably very familiar with.

Well the answer is yes, you can. They are called Helper Classes, but they are defined a little bit differently from how you might expect, and from how it is done in Java.

Here's a simple example of a helper class:
package helper
{
    public class MainClass
    {
        public function MainClass() {
            var helperCls:HelperClass1 = new HelperClass1();
        }
    }
}
    
import mx.controls.TextInput;

class HelperClass1 extends TextInput 
{
    public function HelperClass1() {
    }
}
Notice that the helper class is defined outside of the package wrapper. And yes, multiple helper classes are allowed in the same file. One thing that surprised me was how the helper class has its own separate section for imports that are also outside of the package block.

Note that helper classes are only available to the main class, and cannot be accessed outside of that file. Even a subclass can't use helper classes belonging to its superclass.

Here is another website which has some more information on this topic:
Class syntax in ActionScript 3.0.

1 comment:

ReflexMan said...

They also know as inner Class