片段#1:
public abstract class SuperClass {
protected int i;
}
public class SubClass extends SuperClass {
public void method() {
i = doAnythingWithI(i); // for example
}
}
片段#2:
public abstract class SuperClass {
private int i;
protected int getI() {
return i;
}
protected int setI(int i) {
this.i = i;
}
}
public class SubClass extends SuperClass {
public void method() {
setI(doAnythingWithI(getI())); // for example
}
}
为什么要使用一个片段而不是另一个片段?最常见的处理方式是什么?