继续走这重构一书的复习之路,接着便是内联,除了内联变量,当然还有内联函数。
快捷键
Mac: alt
+command
+M
Windows/Linux: Ctrl
+Alt
+M
鼠标: Refactor | Inline
以之前的提炼函数为例
public class extract {
public void method() {
int one = 1;
int two = 2;
int three = add(one, two);
int four = add(one, three);
}
private int add(int one, int two) {
return one + two;
}
}
在add(one,two)
很愉快地按上个快捷键吧,就会弹出
再轻轻地回车,Refactor就这么结束了。。
以书中的代码为例
double basePrice = anOrder.basePrice();
return (basePrice > 1000);
同样的,按下Command
+alt
+N
return (anOrder.basePrice() > 1000);
对于python之类的语言也是如此
def inline_method():
baseprice = anOrder.basePrice()
return baseprice > 1000
在Intellij IDEA中对于内联函数的使用可能会更简单些,因为它据说是很智能的。
围观我的Github Idea墙, 也许,你会遇到心仪的项目