// 方法参数较多
public void test(int width, int height, int length) {
func1(width, height, length);
func2(width, height, length);
}
private void func1(int width, int height, int length) {
//...
}
private void func2(int width, int height, int length) {
//...
}
改为
// 参数封装成对象形式
public void test(Rectangle rect) {
func1(rect);
func2(rect);
}
private void func1(Rectangle rect) {
int width = rect.getWidth();
int height = rect.getHeight();
int length = rect.getLength();
//...
}
private void func2(Rectangle rect) {
int width = rect.getWidth();
int height = rect.getHeight();
int length = rect.getLength();
//...
}
问题来了:
参数封装成对象的形式,这样每个方法都有一段重复代码:从rect对象中获取属性
int width = rect.getWidth();
int height = rect.getHeight();
int length = rect.getLength();
还不如第一种写的代码少