How to call a function inside a private class whose name is identical with another function?

StackOverflow https://stackoverflow.com/questions/23618816

  •  21-07-2023
  •  | 
  •  

سؤال

Consider following class:

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

I am getting compile time error The method operate() is not applicable for the arguments (int, int).

Is there a way to call the second operate() function?

هل كانت مفيدة؟

المحلول

Is there a way to call the second operate() function?

Yes - you can qualify it with Foo.this to refer to the enclosing instance of Foo:

operatedNumber = Foo.this.operate(x,y);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top