开发者

Java and skipping a level with super [duplicate]

开发者 https://www.devze.com 2023-03-13 17:36 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: Why is super.super.method(); not allowed in Java?
This question already has answers here: Closed 11 years ago.

Possible Duplicate:

Why is super.super.method(); not allowed in Java?

If you have a class that derives from a class that derives from another is there anyway for me to call the super.super method and not the overri开发者_StackOverflow社区dden one?

class A{
    public void some(){}
} 

class B extends A{
    public void some(){}
} 

class C extends B{
    public void some(){I want to call A.some();}
} 


See: Why is super.super.method(); not allowed in Java?


@tgamblin is right but here is a workaround :

class A{
    public void some(){ sharedCode() }
    public final void someFromSuper(){ sharedCode() }

    private void sharedCode() { //code in A.some() }
} 

class B extends A{
    @Override
    public void some(){}
} 

class C extends B{
    @Override
    public void some(){
     //I want to call A.some();
     someFromSuper();
    }
} 

Create a second version of your method in A that is final (not overridable) and call it from C.

This is actually a poor design, but sometimes needed and used inside JDK itself.

Regards, Stéphane

0

精彩评论

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