Eclipse:提取方法功能失败

发布于 2024-12-17 04:16:40 字数 303 浏览 0 评论 0原文

为了使冗长的方法具有更好的可读性,我想用一个方法替换重复使用的代码块(仅赋值)。因此,我选择了该代码块并运行 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

卸妝后依然美 2024-12-24 04:16:40

Eclipse 希望所有变量都用作参数,并返回一个或不返回在提取的块中修改过的变量。

您问题是这样的构造,

void f2() {
        int a,b;
        int foo=0;                           // selection start
        double[] bar = new double[10];
        for ( int i = 0 ; i < bar.length ; i++ ) {
            bar[i] = foo;
        }
        foo = 0;                             // selection end

        a = foo;
        b = (int) bar[0];
    }

因为在进一步的语句中需要两个变量(foo,bar),所以它们不能作为一个值返回。
您可以返回一个包含 foo 和 bar 的类。

将它们声明为成员变量的效果

public class Refactor {
    int foo ;
    double[] bar; 

    void f2() {
        for ( int i = 0 ; i < bar.length ; i++ ) {        // selection start
            bar[i] = foo;
        }
        foo = 0;                                          // selection end
    }
}

如下:

void f2() {
        int foo=0;                                  // selection start
        double[] bar = new double[10];
        for ( int i = 0 ; i < bar.length ; i++ ) {
            bar[i] = foo;
        }
        foo = 0;                                    // selection end
    }

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

void f2() {
        int a,b;
        int foo=0;                           // selection start
        double[] bar = new double[10];
        for ( int i = 0 ; i < bar.length ; i++ ) {
            bar[i] = foo;
        }
        foo = 0;                             // selection end

        a = foo;
        b = (int) bar[0];
    }

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

public class Refactor {
    int foo ;
    double[] bar; 

    void f2() {
        for ( int i = 0 ; i < bar.length ; i++ ) {        // selection start
            bar[i] = foo;
        }
        foo = 0;                                          // selection end
    }
}

as well as this:

void f2() {
        int foo=0;                                  // selection start
        double[] bar = new double[10];
        for ( int i = 0 ; i < bar.length ; i++ ) {
            bar[i] = foo;
        }
        foo = 0;                                    // selection end
    }
ペ泪落弦音 2024-12-24 04:16:40

这些只是赋值还是声明?因为看起来它们是声明。当您尝试将它们提取到方法中时,变量将仅在新方法中可见,因此 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文