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

时间:2014-05-12 21:02:23

标签: java function compiler-errors

考虑以下课程:

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);
    }
}

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

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

1 个答案:

答案 0 :(得分:8)

  

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

是的 - 您可以使用Foo.this对其进行限定,以引用Foo的封闭实例:

operatedNumber = Foo.this.operate(x,y);