PDA

View Full Version : super.super.someMethod()



dr_tchock
December 15th, 2009, 10:31 AM
Is it possible to call a method in the super class of a super class? I tried the above and it doesn't work, unsurprisingly.

Only way I've been able to do it is to store a reference to a super.someMethod in the super Class (eg. var superMethod: Function = super.someMethod) and call that instead.

Seems reasonable and it works but wonder if there is an in-built method for doing this?

senocular
December 15th, 2009, 10:58 AM
No built in method - this is one of the many limitations of ActionScript.

IQAndreas
December 15th, 2009, 11:25 AM
Do you have a specific purpose for this, or are you just thinking out loud?

I have a few ideas if it's for a plan, and if so, elaborate further.

dr_tchock
December 15th, 2009, 11:51 AM
Do you have a specific purpose for this, or are you just thinking out loud?

I have a few ideas if it's for a plan, and if so, elaborate further.

Yeah I have a specific use for it, I used the solution in the OP.

Basically, I make a lot of drag n' drop style apps for use in education. Consequently, I have a SimpleDragger class that I extend whenever I need something draggy/droppy that contains the basic methods for this sort of behaviour (animation, storing original position, collision detection, etc).

I'm doing a bunch of these things at the moment, with some similiar activities. For one activity, I wanted all of the behaviour of the main Dragger (sub of BasicDragger) Class I've written specifically for this project, except for one method - which I wanted to behave like the original BasicDragger class instead.

Hope this makes sense..

IQAndreas
December 15th, 2009, 01:49 PM
Hope this makes sense..
Um... Not quite. I get the basic gist, but I'm still a little unclear on the structure of those classes.


However, one way to make several different versions of the function are to use Namespaces.
http://help.adobe.com/en_US/AS3LCR/Flash_10.0/Namespace.html
I'm still learning to use them, so I might be a little off on the syntax, but this is the main idea:

package components.dragging
{
public class BaseDragger
{
//Note that this property will be inherited by all subclasses
//You can use any string in the Namespace constructor
public const baseDragger:Namespace = new NameSpace("components.dragging.BaseDragger");

public function BaseDragger():void
{
trace("BaseDragger initialized");
}


baseDragger function startDrag(target):void
{
//Drag target according to "baseDragger" standards
}

//Have the public function by default point to "BaseDragger's startDrag"
public function startDrag(target):void
{
this.baseDragger::startDrag(target);
}

}
}

package components.dragging
{
import components.dragging.BaseDragger
public class MainDragger extends BaseDragger
{
//Note that this property will be inherited by all subclasses
public const mainDragger:Namespace = new NameSpace("components.dragging.MainDragger");

public function MainDragger():void
{ trace("MainDragger initialized"); }


mainDragger function startDrag(target):void
{
//Drag target according to "mainDragger" standards
}

//Have the public function by default point to this class's startDrag
//Note that this is an override function,
//while the BaseDragger's function was the one you are now overriding
public override function startDrag(target):void
{
this.mainDragger::startDrag(target);
}

}
}

package components.dragging
{
import components.dragging.MainDragger
public class FinalDragger extends MainDragger
{
//Actually, this namespace might not even be needed in this class
public const finalDragger:Namespace = new NameSpace("components.dragging.FinalDragger");

public function FinalDragger():void
{ trace("FinalDragger initialized"); }


//NOTE!!!
//Now, instead of creating your own "starDrag" function,
//you are pointing to "baseDragger"s startDrag function!
public override function startDrag(target):void
{
//Refer to baseDragger!!
this.baseDragger::startDrag(target);

//You could aslo create a new "finalDragger function startDrag()"
//and instead point to the base class from there to follow the
//same pattern as the previous classes
}

}
}

Do you understand what the code is doing, or shall I elaborate further?


Another option is to just make a "Dragger" interface. Then you avoid override statements, but then you might have to rewrite a lot of the code, and unless you use a main "DragController" class or something, you might have a lot of redundant code that is the same in both subclasses.


Otherwise, can I have a look at your code and perhaps I can devise a solution that fits your current needs just right? Perhaps the best solution is simply to make all the Dragging classes static, but it all depends on your use.

dr_tchock
December 16th, 2009, 05:44 AM
Thansk for the reply, namespaces are on my to-do list of things to learn. I shall have a closer look at this in future - I get the idea just not sure of the implementation until I try it.

As for the current problem, as mentioned, I solved it by storing a reference to the method I wanted in the superclass and accessed it via that:



public class SimpleDragger extends Sprite
{
public function drag():void
{
// do dragging stuff
}

public function drop():void
{
// do dropping stuff
}
}





public class Dragger extends SimpleDragger
{
public var superSuperDrop:Function = super.drop;

override public function drag():void
{
super.drag();
// do more dragging stuff
}

override public function drop():void
{
super.drop();
// code for swapping dragger with dropper
}
}





public class AnotherDragger extends Dragger
{
override public function drag():void
{
super.drag();
// do more dragging stuff
}

override public function drop():void
{
// don't want swapping behaviour so call superclass' superclass drop method
superSuperDrop();
// do more dropping stuff
}
}

Krilnon
December 16th, 2009, 05:55 AM
As for the current problem, as mentioned, I solved it by storing a reference to the method I wanted in the superclass and accessed it via that:

That's the approach I would have taken… so I support that solution! :P You can do something very much like that to implement multiple inheritance on top of a language with single inheritance.