开发者

Is it possible to access a superclass's superclass in flex/as3?

I'm trying to override a subclass's method but I think I need access to the superclass one higher up in the hierarchy. So I'd like to do something like super.super.methodName();

FYI, the actual problem I'm开发者_如何学Go trying to solve should be explained by my code below:

public class A extends UIComponent{
    override protected function keyDownHandler(event:KeyboardEvent):void{
        super.keyDownHandler(event);

        if(event.keyCode==46)
            remove();

    }
}

public class B extends A{
    override protected function keyDownHandler(event:KeyboardEvent):void{

        if(event.keyCode==46 && removeable==true)
            remove();
                    else
                super.super.keyDownHandler(event);

    }
}

If I use the class A's KeyDownHandler method, you will see that remove() is called regardless. However, the docs state that I need to call UIComponent.keyDownHandler whenever I subclass UIComponent. Of course this works for the first subclass, but not that subclass's subclasses.

I realise that I could put the functionality all into A with something like if(this is B){...} but that seems to me to be a hack.


You could add another method to class A, let's call it superKeyDownHandler whose only purpose would be to call some method from superclass.

protected function superKeyDownHandler(event:KeyboardEvent):void{
    super.keyDownHandler(event);
}

And then what you want to achieve would be as easy as calling

super.superKeyDownHandler(event);

This is just a way around however not the actual clean solution, so use it at your own risk :)

UPDATE:

Something maybe cleaner would involve overriding keyDownHandler content of class A. So in class A you would have:

public class A{
    override protected function keyDownHandler(event:KeyboardEvent):void{
        super.keyDownHandler(event);
        overridableContent();
    }

    protected function overridableContent(){
        //do class A specific things here
        if (event.keyCode==46)
            remove();
    }
}

public class B extends A{
    override protected function overridableContent(){
        //do class B specific things here
        if (event.keyCode==46 && removeable==true)
            remove();
    }
}

Calling something like new B().keyDownHandler(event) now will cause calling keyDownHandler as it is defined in class A with content taken from class B.


What I gather from your question is that, you want to use the functionality given by the superclasses' superclass, and not by the superclass itself. Am I right cammil?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜