|
-
self-portrait
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?
-
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/
-
self-portrait
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
}
-
 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|