2

如果您无意在 C++ 中使用参数,则只需省略变量的名称,但将类型保留在函数声明中。这样做是一种轻量级的方法,可以向编译器发出信号,表明不打算使用给定的参数。

这可以在Java中复制吗?

场景是这样的:我不想@SuppressWarnings("unused")为每个按钮侦听器充斥我的代码(因为按钮侦听器通常会忽略View v他们收到的),但我想总体上警告我这个愚蠢的错误,比如说,不通过参数初始化成员变量。

4

2 回答 2

0

我想不出像 C++ 选项这样的东西,但您可以扩展/实现您的按钮侦听器并添加一个不带参数的新方法,并在原始事件方法中调用该方法。这样,您只需抑制一次警告。

public void someEvent(Parameter p) { someEvent(); }

public void someEvent() { }

于 2013-04-16T17:10:35.280 回答
0

In addition to creating an adapter like @Farzad suggested, you might want to also check your compiler settings in your IDE. For example, I use Eclipse and there are error/warning compiler settings under Window->Preferences->Java Compiler->Errors/Warnings. Within that dialog you can set UnnecessaryCode->Value of parameter is not used->Ignore in overriding and implementing methods.

Having that option checked automatically ignores those unused params when they are from a method that you are implementing/overriding such as in your listener case. I generally find that to be sufficient without needing to create an adapter.

于 2013-04-16T17:24:12.740 回答