AS3.0 Abstracts

Another thing I needed was a strict abstract class in AS3, again, public only constructors, and the absence of the "abstract" keyword from ActionScript meant a workaround.

Actionscript:
  1. package
  2. {
  3.     import flash.utils.getDefinitionByName;
  4.     import flash.utils.getQualifiedClassName;
  5.    
  6.     public class AbstractExample
  7.     {
  8.         public function AbstractExample()
  9.         {
  10.             if( Class( getDefinitionByName( getQualifiedClassName( this ) ) ) == AbstractExample ) throw new Error( 'AbstractExample must be extended' );
  11.            
  12.             // rest of constructor here...
  13.         }      
  14.     }
  15. }

I like this solution best as it doesn't rely on strings for comparison as in Tink's example. His method is shorter, and easier for typing. It's down to personal preference really.

1 Comment so far

  1. Denis Kolyako / October 21st, 2007 3:36 pm

    This method slowly :)
    This method is much better:
    package {
    import flash.utils.getQualifiedClassName;

    [ExcludeClass]
    public class AbstractClass {
    public function AbstractClass() {
    // super() here
    if ((this as Object).constructor === AbstractClass) {
    throw new ArgumentError('ArgumentError: '+getQualifiedClassName(this)+' class cannot be instantiated.');
    }
    }
    }
    }

Leave a reply

*
To prove that you're not a bot, enter this code
Anti-Spam Image