728x90
๋ฐ์ํ
๐ฏ ์์ดํ
23. ํ๊ทธ ๋ฌ๋ฆฐ ํด๋์ค๋ณด๋ค๋ ํด๋์ค ๊ณ์ธต๊ตฌ์กฐ๋ฅผ ํ์ฉํ๋ผ.
ํ๊ทธ ๋ฌ๋ฆฐ ํด๋์ค - ํด๋์ค ๊ณ์ธต๊ตฌ์กฐ๋ณด๋ค ํจ์ฌ ๋์๋ค!
public class Figure { enum Shape { RECTANGLE, CIRCLE }; // ํ๊ทธ ํ๋ - ํ์ฌ ๋ชจ์์ ๋ํ๋ธ๋ค. final Shape shape; // ๋ค์ ํ๋๋ค์ ๋ชจ์์ด ์ฌ๊ฐํ(RECTANGLE)์ผ ๋๋ง ์ฐ์ธ๋ค. double length; double width; // ๋ค์ ํ๋๋ ๋ชจ์์ด ์(CIRCLE)์ผ ๋๋ง ์ฐ์ธ๋ค. double radius; // ์์ฉ ์์ฑ์ Figure(double radius) { shape = Shape.CIRCLE; this.radius = radius; } // ์ฌ๊ฐํ์ฉ ์์ฑ์ Figure(double length, double width) { shape = Shape.RECTANGLE; this.length = length; this.width = width; } double area() { switch(shape) { case RECTANGLE: return length * width; case CIRCLE: return Math.PI * (radius * radius); default: throw new AssertionError(shape); } } }
1๏ธโฃ ํ๊ทธ ๋ฌ๋ฆฐ ํด๋์ค๋ ์ฅํฉํ๊ฒ, ์ค๋ฅ๋ฅผ ๋ด๊ธฐ ์ฝ๊ณ , ๋นํจ์จ์ ์ด๋ค.
2๏ธโฃ ํ๊ทธ ๋ฌ๋ฆฐ ํด๋์ค๋ ํด๋์ค ๊ณ์ธต๊ตฌ์กฐ๋ฅผ ์ด์คํ๊ฒ ํ๋ด๋ธ ์๋ฅ์ผ ๋ฟ์ด๋ค.
์ฐธ๊ณ ์๋ฃ
Joshua Bloch, ใEffective Java 3/Eใ, ๊ฐ์๋งต์ ์ฎ๊น, ํ๋ก๊ทธ๋๋ฐ์ธ์ฌ์ดํธ(2018)
http://www.kyobobook.co.kr/product/detailViewKor.laf?ejkGb=KOR&mallGb=KOR&barcode=9788966262281&orderClick=LEa&Kc=
728x90
๋ฐ์ํ