[英]How can I use Extract Method on code that has different constants?
鉴于我有如下代码:
void foo() {
String str = "hello";
a(str, 1);
b(str, true);
a(str, 2);
b(str, false);
}
我想提取一个新的方法c
像:
void foo() {
String str = "hello";
c(str, 1, true);
c(str, 2, false);
}
但是,自动提取方法重构将仅提取a
/ b
对之一。 我的猜测是它不喜欢不同的常数。 我可以通过以下方法解决此问题:首先提取局部变量,然后提取方法,然后内联先前提取的变量,但是我仍然必须手动查找所有实例。 通过这么多的工作,我不妨在查看每个零件时自己进行全部更改。
有什么技巧让我知道让Eclipse更加努力地搜索以提取这种类型的代码吗?
是的,您需要将常量提取到变量中,然后将其放在顶部以告诉工具将其作为参数传递给提取的方法。
void foo() {
String str = "hello";
int constant = 1;
boolean boolValue = true;
a(str, constant);
b(str, boolValue);
constant = 2;
boolValue = false;
a(str, constant);
b(str, boolValue);
}
进行提取方法应提供以下内容:
public void c(String str, int constant, boolean boolVal){
a(str, constant);
b(str, boolVal);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.