A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: Simulating virtual methods?

  1. #1
    self-portrait Kianis's Avatar
    Join Date
    Feb 2004
    Location
    Stockholm, Sweden
    Posts
    425

    Simulating virtual methods?

    I'd like the decendants of my class to be forced to implement their own versions of certain methods. As you can't make virtual methods or classes in flash you have to be creative though, that's why I'm asking if anyone has encountered this problem before and possibly solved it in a clever way?

    I tried making an interface, but as the base class got the methods defined (as it will be calling them) the compiler won't complain when the decendant implements the interface without its own method definitions.

    What I got so far is this:
    Code:
    class A
    {
    	public function A ()
    	{
    		myFunction ();
    	}
    	public function myFunction ()
    	{
    		trace ( "myFunction needs to be written!" );
    	}
    }
    Code:
    class B extends A
    {
    }
    Now if class B hasn't got a myFunction() the base class will trace the error message. It's not a very elegant solution. It won't even tell which class it is that is lacking the function. Help?
    // Mazapán, my portfolio

  2. #2
    Member
    Join Date
    Jul 2005
    Location
    Brisbane, Australia
    Posts
    66
    Have a look at this site. I came across it while looking for a way to implement pure virtual functions in actionscript, i didn't end up using tho as it seem to much of hassle.

    http://nodename.com/blog/2005/09/19/abstract-classes/

  3. #3
    self-portrait Kianis's Avatar
    Join Date
    Feb 2004
    Location
    Stockholm, Sweden
    Posts
    425
    Oh, that's perfect!
    Thanks a lot

    For those not wanting to read the whole article, here's the simplified solution:

    Code:
    class A
    {
    	private var sub :InterfaceA;
    	public function A ()
    	{
    		sub = InterfaceA ( this );
    		if ( sub == null ) trace ( "Decendant must override methods declared in InterfaceA." );
    		sub.myFunction ( 3 );
    	}
    }
    Code:
    class B extends A implements InterfaceA
    {
    }
    Code:
    interface InterfaceA
    {
    	public function myFunction ( x :Number ) :Void
    }
    // Mazapán, my portfolio

  4. #4
    Member
    Join Date
    Jul 2005
    Location
    Brisbane, Australia
    Posts
    66
    Quote Originally Posted by Kianis
    Oh, that's perfect!
    Thanks a lot
    Awesome, good to hear.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center