如何在名称与另一个函数相同的私有类中调用函数?

pad*_*wan 2 java compiler-errors function

考虑以下课程:

class Foo
{
    private class Bar
    {
        int operatedNumber;
        Bar(int x, int y)
        {
            operatedNumber = operate(x,y);
        }
        int operate(int x)
        {
           return x*2; 
        }
    }
    public int operate(int x, int y)
    {
        return x+y;
    }
    public Foo()
    {
        Bar b = new Bar(3,5);
    }
}
Run Code Online (Sandbox Code Playgroud)

我收到编译时错误 The method operate() is not applicable for the arguments (int, int).

有没有办法调用第二个operate()功能?

Jon*_*eet 8

有没有办法调用第二个operation()函数?

是的 - 您可以Foo.this通过引用以下内容来限定它Foo:

operatedNumber = Foo.this.operate(x,y);
Run Code Online (Sandbox Code Playgroud)