Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Eclipse/Java中的重构:应用;“提取方法”;(Alt&x2B;Shift&x2B;M)之后_Java_Eclipse_Refactoring - Fatal编程技术网

Eclipse/Java中的重构:应用;“提取方法”;(Alt&x2B;Shift&x2B;M)之后

Eclipse/Java中的重构:应用;“提取方法”;(Alt&x2B;Shift&x2B;M)之后,java,eclipse,refactoring,Java,Eclipse,Refactoring,我想知道是否可以通过调用前面提取的方法来替换某些代码 例如,我有一个具有类似模式的类: public class ExtractMethodDemo { public void doSequence() { long n1; n2; // Compute and print count n1 = 70; n2 = compute(n1); // &

我想知道是否可以通过调用前面提取的方法来替换某些代码

例如,我有一个具有类似模式的类:

public class ExtractMethodDemo {
    public void doSequence() {
        long n1; n2;
        // Compute and print count
        n1 = 70;
        n2 = compute(n1);                                        // <-- 1st
        System.out.printf("Input %4s, output = %s.%n", n1, n2);  // <-- occurrence
        ....
        // Compute and print count again
        n2 = n2 % 100;
        n1 = compute(n2);                                        // <-- Nth
        System.out.printf("Input %4s, output = %s.%n", n2, n1);  // <-- occurrence
    }
}
之后是否可以重构铰孔序列:

public void doSequence() {
    long n1; n2;
    // Compute and print count
    n1 = 70;
    n2 = doAll(n1);
    // ....
    // Compute and print count again
    n2 = n2 % 100;
    n1 = doAll(n2);    // <--- would be great to apply same refactoring afterwards
}
public void doSequence(){
长n1;n2;
//计算和打印计数
n1=70;
n2=doAll(n1);
// ....
//再次计算并打印计数
n2=n2%100;

n1=doAll(n2);//可能比重新内联代码好一点,就是在新代码的整个主体上提取方法,选中
Replace Additional executes
,然后从原始调用内联新方法。这样,您选择错误行进行提取的风险就更小了

更新:以下是一个示例:

你从

extractableCode(1);
extractableCode(2);
extractableCode(3);
并提取原始块,留下

extractedMethod(1);
extractableCode(2);
extractableCode(3);
...
function extractedMethod(int i) {
    extractableCode(i);
}
您的方法是内联extractedMethod,然后用Replace All executions重复提取。我建议您从内部提取
extractedMethod()

然后将原始调用内联到第一个提取的方法:

secondExtractedMethod(1);
secondExtractedMethod(2);
secondExtractedMethod(3);
...
function secondExtractedMethod(int i) {
    extractableCode(i);
}

然后,可能会重命名第二个提取的方法。它与您最初建议的方法只有一点点不同,但可能更可靠一些。

作为最后手段,我可以重新内联所有代码,然后再次提取该方法,但这并不优雅……谢谢。请原谅,我不理解操作的顺序,特别是什么是“新代码的整个主体”。在我的示例中…1.我选择了两个遗忘的行(第n次出现)2.提取到一个新方法(例如doAll2)。3?
extractedMethod(1);
secondExtractedMethod(2);
secondExtractedMethod(3);
...
function extractedMethod(int i) {
    secondExtractedMethod(i);
}
function secondExtractedMethod(int i) {
    extractableCode(i);
}
secondExtractedMethod(1);
secondExtractedMethod(2);
secondExtractedMethod(3);
...
function secondExtractedMethod(int i) {
    extractableCode(i);
}