주생성자
-
엘레강트 오브젝트 - 주 생성자 재활용책책책 책을 읽읍시다/프로그래밍 2023. 8. 1. 00:07
응집도가 높고 견고한 클래스에는 적은 수의 메서드와 상대적으로 더 많은 수의 생성자가 존재한다. 생성자의 개수가 더 많을수록 클래스는 더 개선되고, 사용자 입장에서 클래스를 더 편하게 사용할 수 있다. 아래와 같이 Cash란 클래스가 있고 다양한 방법으로 Cash 인스턴스를 생성하고 싶은 경우 다음과 같이 여러 종류의 생성자를 활용한다. public class Cash { private int dollars; public Cash(int dollars) { this.dollars = dollars; } public String usd() { return String.format("$ %d", this.dollars); } } new Cash(30); new Cash("$29.95"); new Cash(2..