ActionScript 3 Object Oriented Programming "Overloaded" Constructor via static method


Example

Constructor overloading is not available in As3.

In order to provide a different way to retrieve an instance of a class, a public static method can be provided to serve as an alternative "constructor".

An example for that is flash.geom.Point, which represents a 2D point object. The coordinates to define the point can be

  • cartesian in the regular constructor

    public function Point(x:Number = 0, y:Number = 0) 
    

    example usage:

    var point:Point = new Point(2, -.5);
    
  • polar in a static method

    public static function polar(len:Number, angle:Number):Point
    

    example usage:

    var point:Point = Point.polar(12, .7 * Math.PI);
    

    Because it is not an actual constructor, there's no new keyword.