我不太确定为什么在这种情况下应该使用 getter。考虑以下示例:
public class Coin {
private String sideUp;
Coin() {
sideUp = "heads";
}
protected String getSideUp() {
return sideUp;
}
protected void toss(String sideUp) {
// some code to randomly decide heads or tails
this.sideUp = sideUp;
}
public static void main(String[] args) {
Coin coin = new Coin();
System.out.printf("Initial side %s\n", coin.sideUp);
// System.out.printf("Initial side is %s\n, coin.getSideUp);
coin.toss(coin.sideUp);
// coin.toss(coin.getSideUp);
System.out.printf("Side Up is %s\n", coin.sideUp);
// System.out.printf("Side Up is is %s\n, coin.getSideUp);
}
}
coin.SideUp
和 和有什么不一样coin.getSideUp
?在这种情况下,是否是不同方法的问题?