直接访问一个字段,但与字段之间的耦合关系逐渐变得笨拙
private int low;
private int high;
boolean includes(int arg) {
return arg >= low && arg <= high;
}
重构:为这个字段建立get/set函数,并以这些函数访问字段
private int low;
private int high;
public int getLow() {
return low;
}
public int getHigh() {
return high;
}
public boolean includes(int arg) {
return arg >= getLow() && arg <= getHeigh();
}