MyungHoon_Ju

불변객체

Cash price = new Cash();
50줄 뒤
price.setDollars(29);
30줄 뒤
price.setCents(95);
25줄 뒤
System.err.print(price); // “$29.95”

Cash price = new Cash(29, 95);

class Cash { // 불변객체
  private int dollars;
  public Cash multiply(int factor) {
   return new Cash(this.dollars * factor);
  }
}