gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 12:50:08 24 4
gpt4 key购买 nike

考虑以下类(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);
}
}

我收到编译时错误操作()方法不适用于参数(int,int)。

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

最佳答案

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

是的 - 您可以使用 Foo.this 限定它以引用 Foo 的封闭实例:

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

关于java - 如何在私有(private)类中调用与另一个函数名称相同的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23618816/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com