๋ณธ๋ฌธ์œผ๋กœ ๊ฑด๋„ˆ๋›ฐ๊ธฐ

๐ŸŒˆ Chapter 7: ์‚ฌ๊ณผ์™€ ์˜ค๋ Œ์ง€

public void testEquality() {
assertTrue(new Dollar(5).equals(new Dollar(5)));
assertFalse(new Dollar(5).equals(new Dollar(6)));
assertTrue(new Franc(5).equals(new Franc(5)));
assertFalse(new Franc(5).equals(new Franc(6)));
assertFalse(new Franc(5).equals(new Dollar(5)));
}

์‹คํŒจํ•œ๋‹ค. Dollar๊ฐ€ Franc์ด๋ผ๋Š”๊ตฐ. ๋™์น˜์„ฑ ์ฝ”๋“œ์—์„œ๋Š” Dollar๊ฐ€ Franc๊ณผ ๋น„๊ต๋˜์ง€ ์•Š๋Š”์ง€ ๊ฒ€์‚ฌํ•ด์•ผ ํ•œ๋‹ค. ๋‘ ๊ฐ์ฒด์˜ ํด๋ž˜์Šค๋ฅผ ๋น„๊ตํ•จ์œผ๋กœ์จ ์ด๋Ÿฌํ•œ ๊ฒ€์‚ฌ๋ฅผ ์‰ฝ๊ฒŒ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค. ์˜ค์ง ๊ธˆ์•ก๊ณผ ํด๋ž˜์Šค๊ฐ€ ์„œ๋กœ ๋™์ผํ•  ๋•Œ๋งŒ ๋‘ Money๊ฐ€ ์„œ๋กœ ๊ฐ™์€ ๊ฒƒ์ด๋‹ค.

public boolean equals(Object object) {
Money money = (Money) object;
return amount == money.amount && getClass().equals(money.getClass());
}

๋ชจ๋ธ ์ฝ”๋“œ์—์„œ ํด๋ž˜์Šค๋ฅผ ์ด๋Ÿฐ ์‹์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ ์ข€ ์ง€์ €๋ถ„ํ•ด ๋ณด์ธ๋‹ค. ์ž๋ฐ” ๊ฐ์ฒด์˜ ์šฉ์–ด๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค ์žฌ์ • ๋ถ„์•ผ์— ๋งž๋Š” ์šฉ์–ด๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ๋‹ค. ํ•˜์ง€๋งŒ ํ˜„์žฌ๋Š” ํ†ตํ™” ๊ฐœ๋… ๊ฐ™์€ ๊ฒŒ ์—†๊ณ , ํ†ตํ™” ๊ฐœ๋…์„ ๋„์ž…ํ•  ์ถฉ๋ถ„ํ•œ ์ด์œ ๊ฐ€ ์—†์–ด ๋ณด์ด๋ฏ€๋กœ ์ž ์‹œ ์ด๋Œ€๋กœ ๋‘์ž.

์ด์ œ ์ •๋ง ๊ณตํ†ต times() ์ฝ”๋“œ๋ฅผ ์ฒ˜๋ฆฌํ•ด์•ผ ํ•  ๋•Œ๋‹ค. ๋”ฐ๋ผ์„œ ํ˜ผํ•ฉ๋œ ํ†ตํ™” ๊ฐ„์˜ ์—ฐ์‚ฐ์— ๋Œ€ํ•ด ๋‹ค๋ฃจ์–ด์•ผ ํ•œ๋‹ค.