如何从内部类访问隐藏的外部类变量? [英] How to access shadowed Outer class variable from Inner class?

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

问题描述

这不是直截了当的问题.在我的例子中,外部类变量和内部类 setter 方法的参数名称是相同的.喜欢:

This is not straight forward question. In my case the outer class variable and the inner class setter method's argument name is same. like:

class Problem {
    String s;
    int p;
    class Inner {
        String testMethod() {
         return  s = "Set from Inner";
        }
        void setP(int p)
        {
            p=p;  //it will do self assignment
        }
    }


}

现在我不能用 this.p=p 初始化外部类实例变量 p,因为它表示内部类.再次我不能做 Problem.p=p; 它得到一个错误.现在我如何分配外部 p,保持内部类方法 setP(int p) 的参数同名p?

now I cant initialize outer class instance variable p with this.p=p as it indicates the inner class. again I cannot do Problem.p=p; it gets an error. Now how can I assign outer p, keeping the inner Class method setP(int p)'s argument the same name p ?

推荐答案

这是您可以/应该这样做的方式:

This is how you can/should do it:

Problem.this.p

这篇关于如何从内部类访问隐藏的外部类变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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