如何从嵌套类访问超类方法? [英] How to access a superclass method from a nested class?

查看:107
本文介绍了如何从嵌套类访问超类方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望此代码解释问题:

I hope this code explains the problem:

class Foo {
    void a() { / *stuff */ }
}

class Bar extends Foo {
    void a() { throw new Exception("This is not allowed for Bar"); }

    class Baz {
        void blah() {
            // how to access Foo.a from here?
        }
    }
}

我知道我可能是做错了,因为继承也许不应该以这种方式使用。但这是我情况下最简单的方法。而且,除此之外,我只是好奇。有可能吗?

I know that I may be doing something wrong, because inheritance perhaps shouldn't be used in such way. But it's the easiest way in my situation. And, beside that, I'm just curious. Is it possible?

推荐答案

Bar.super.a()出现工作。

JLS第15.12节


ClassName。超级NonWildTypeArguments_opt标识符(ArgumentList_opt)

ClassName . super . NonWildTypeArguments_opt Identifier ( ArgumentList_opt )

是一个有效的MethodInvocation

is a valid MethodInvocation

这篇关于如何从嵌套类访问超类方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆