Eclipse:提取方法功能失败
为了使冗长的方法具有更好的可读性,我想用一个方法替换重复使用的代码块(仅赋值)。因此,我选择了该代码块并运行 Eclipse 的 Extract Method 功能,但失败并出现以下错误:
返回值不明确:选定的块包含多个对局部变量的赋值。受影响的变量是:
int foo 双[]栏
我该如何解决这个问题?它应该是一个简单的 void
方法,执行几个任务,我不确定 Eclipse (3.6.2) 抱怨什么。
For better readability of a lengthy method, I'd like to replace a repeatedly used block of code (only assignments) with a method. I therefore selected the block of code and ran Eclipse's Extract Method feature, but that failed with this error:
Ambiguous return value: Selected block containsmore than one assignment to local variables. Affacted variables are:
int foo double[] bar
How can I fix this? It should be a simple void
method doing a couple of assignments, I'm not sure what Eclipse (3.6.2) complains about.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Eclipse 希望所有变量都用作参数,并返回一个或不返回在提取的块中修改过的变量。
您问题是这样的构造,
因为在进一步的语句中需要两个变量(foo,bar),所以它们不能作为一个值返回。
您可以返回一个包含 foo 和 bar 的类。
将它们声明为成员变量的效果
如下:
Eclipse wants all variables that are used as arguments and return one or no variable that was modified within the extracted block.
You issue is a construct like
Since both variables (foo,bar) are needed in further statements they can't be returned as one value.
You could return a class containing foo and bar.
Declareing them as member variables works
as well as this:
这些只是赋值还是声明?因为看起来它们是声明。当您尝试将它们提取到方法中时,变量将仅在新方法中可见,因此 Eclipse 希望返回它们,以使它们在调用块中可用。
如果可能的话,您可以将变量设置为全局变量(即成员变量),然后将赋值代码放入额外的方法中
编辑:
正如stacker指出的,Eclipse希望返回在新方法中修改的所有变量。由于 Java 使用按值调用,这对于基本类型来说是必要的。因为对它们的赋值仅在提取的方法中可见,而在外部不可见。
Are these only assignments or are they also declarations? Because it looks like they were declarations. And when you try to extract them into a method the variables would be visible only in the new method and thus, Eclipse want's to return them, to make them available in the calling blocks.
If it is possible, you could make the variables global (i.e. member variables) and then put the assignment code into an extra method
EDIT:
As stacker pointed out, Eclipse wants to return all variables that were modified in the new method. Since Java uses call-by-value this is necessary for primitive types. Because assignments to them are only visible in the extracted method, but not outside.