본문 바로가기

프로그래밍 공부/java

[Java] 이것이 자바다 6장 확인 문제 1 : this()를 활용해서 중복 코드 제거하기

반응형

Q.Board 클래스의 생성자가 다음과 같이 오버로딩되어 있습니다. 생성자마다 중복 코딩된 부분이 있습니다. this( )를 활용해서 중복 코드를 제거해보세요.

public class Board {
	String title;
	String content;
	String writer;
	String date;
	int hitcount;
	
	Board(String title, String content){
		this.title = title;
		this.content = content;
		this.writer = "로그인한 회원아이디";
		this.date = "현재 컴퓨터 날짜";
		this.hitcount = 0;
	}
	
	Board(String title, String content, String writer){
		this.title = title;
		this.content = content;
		this.writer = writer;
		this.date = "현재 컴퓨터 날짜";
		this.hitcount = 0;
		
	}
	Board(String title, String content, String writer, String date){
		this.title = title;
		this.content = content;
		this.writer = writer;
		this.date = date;
		this.hitcount = 0;
		
	}
	
	Board(String title, String content, String writer, String date, int hitcount){
		this.title = title;
		this.content = content;
		this.writer = writer;
		this.date = date;
		this.hitcount = hitcount;
	}

 

public class Board {
	String title;
	String content;
	String writer;
	String date;
	int hitcount;
	
	Board(String title, String content){
		this.title = title;
		this.content = content;
		this.writer = "로그인한 회원아이디";
		this.date = "현재 컴퓨터 날짜";
		this.hitcount = 0;
	}
	
	Board(String title, String content, String writer){
		this(title, content);
		this.writer = writer;
		
	}
	Board(String title, String content, String writer, String date){
		this(title, content, writer);
		this.date = date;
	}
	
	Board(String title, String content, String writer, String date, int hitcount){
		this(title, content, writer, date);
		this.hitcount = hitcount;
	}
	
		
}
반응형