我很难过。我想通过包含 getter 和 setter 来优化一些代码。这是我到目前为止所拥有的:
public class ThingHolder {
private int updateCounter=0;
private Object theThing;
public Object getThing() {
return theThing;
}
public void setThing(Object theThing) {
this.theThing = theThing;
updateCounter++;
}
public int getUpdateCounter() {
return updateCounter;
}
和:
public class ThingHolderTester {
public static void main(String[] args) {
ThingHolder t = new ThingHolder();
t.setThing="First Object"; t.updateCounter++;
System.out.println("The thing is currently " + t.getThing + " and the ThingHolder has been updated " + t.updateCounter + " times");
t.setThing="Second Object"; t.updateCounter++;
System.out.println("The thing is currently " + t.getThing + " and the ThingHolder has been updated " + t.updateCounter + "times");
}
目前我不断收到错误,在我的 get 和 set 方法上找不到符号。请问有什么帮助吗?