JAVA

[Java] 생성자 오버로딩(Constructor Overloading)

sagesse2021 2021. 10. 28. 17:25
반응형
public class Ex2 {

	public static void main(String[] args) {

		//생성자 오버로딩
		//-메서드 오버로딩과 동일
		//- 생성자 호출 시 다양한 형태의 파라미터를 전달하여
		//-객체를 다양하게 초기화 하는 목적
		Person p = new Person("한국", "홍길동", "901010-1234567");
		p.showPersonInfo();
		
		System.out.println("-----------------------------");
		
		// 문자열 2개만 전달할 경우 이름, 주민번호만 초기화하고
		// 국가는 "대한민국"으로 초기화하는 생성자 호출
		Person p2 = new Person("홍길동", "901010-1234567");
		p2.showPersonInfo();
		
		System.out.println("-----------------------------");
		
		//파라미터 생성자를 하나라도 정의할 경우
		//기본 생성자가 자동으로 생성되지x
		// 따라서, 기본 생성자를 호출해야 하는 경우 직접 기본 생성자를 정의!
		Person p3 = new Person(); 
	}

}

class Person {
	
	String nation;
	String name;
	String jumin;
	
	// 기본 생성자 정의
	public Person() {
		System.out.println("Person()생성자 호출됨");
	}
	
	// 국가는 자동으로 "대한민국" 으로 초기화하고
	// 파라미터 2개(name, jumin)를 전달받아 초기화하는 생성자 정의
	public Person( String newName, String newJumin) {
		System.out.println("Person(String,String) 생성자 호출됨!");
		nation = "한국";
		name = newName;
		jumin = newJumin;
	}
	// 파라미터 3개(nation, name, jumin) 를 전달받아 초기화하는 생성자 정의
	public Person(String newNation, String newName, String newJumin) {
		System.out.println("Person(String,String,String) 생성자 호출됨!");
		nation = newNation;
		name = newName;
		jumin = newJumin;
	}
	
	// 국가명, 이름, 주민번호를 출력하는 showPersonInfo() 메서드 정의
	public void showPersonInfo() {
		System.out.println("국가 : " + nation);
		System.out.println("이름 : " + name);
		System.out.println("주민번호 : " + jumin);
	}
}
/*
Person(String,String,String) 생성자 호출됨!
국가 : 한국
이름 : 홍길동
주민번호 : 901010-1234567
-----------------------------
Person(String,String) 생성자 호출됨!
국가 : 한국
이름 : 홍길동
주민번호 : 901010-1234567
-----------------------------
Person()생성자 호출됨
*/
public class Test2 {

	public static void main(String[] args) {

		MyDate md1 = new MyDate();
		md1.showDate();
		
		MyDate md2 = new MyDate(2022);
		md2.showDate();
		
		MyDate md3 = new MyDate(2021,5);
		md3.showDate();
		
		MyDate md4 = new MyDate(2021,6,30);
		md4.showDate();
	}

}

/*
 * 날짜를 관리하는 MyDate 클래스 정의
 * - 멤버변수 : 연도(year, 정수), 월(month, 정수), 일(day, 정수)
 * - 생성자
 *   1) 기본 생성자 : 2021년 4월 30일로 초기화
 *   2) 파라미터 생성자
 *      2-1. 연도(year)를 전달받아 초기화하고, 4월 30일로 초기화
 *      2-2. 연도(year), 월(month)을 전달받아 초기화하고, 30일로 초기화
 *      2-3. 연도(year), 월(month), 일(day)을 전달받아 초기화
 *      
 */
class MyDate {
	
	int year;
	int month;
	int day;
	
	public MyDate( ) {
		year = 2021;
		month = 4;
		day = 30;
	}
		
	public MyDate(int newYear) {
		year = newYear;
		month = 4;
		day = 30;
	}
	public MyDate(int newYear,int newMonth) {
		year = newYear;
		month = newMonth;
		day = 30;
		
	}
	public MyDate(int newYear,int newMonth, int newDay) {
		year = newYear;
		month = newMonth;
		day = newDay;
	}
	
	public void showDate() {
		System.out.println(year + "년 " + month + "월 " + day + "일 ");
	}
}
/*
2021년 4월 30일 
2022년 4월 30일 
2021년 5월 30일 
2021년 6월 30일
*/
public class Test2_2 {

	public static void main(String[] args) {
		/*
		 * Account() 생성자 호출 시 다음과 같이 초기화 후 출력
		 * - 계좌번호 : 111-1111-111
		 *   예금주명 : 홍길동
		 *   현재잔고 : 0원
		 *   
		 * Account(String) 생성자 호출 시 다음과 같이 초기화 후 출력
		 * - 계좌번호 : 입력받은 계좌번호
		 *   예금주명 : 홍길동
		 *   현재잔고 : 0원
		 *   
		 * Account(String, String) 생성자 호출 시 다음과 같이 초기화 후 출력
		 * - 계좌번호 : 입력받은 계좌번호
		 *   예금주명 : 입력받은 예금주명
		 *   현재잔고 : 0원
		 *   
		 * Account(String, String, int) 생성자 호출 시 다음과 같이 초기화 후 출력
		 * - 계좌번호 : 입력받은 계좌번호
		 *   예금주명 : 입력받은 예금주명
		 *   현재잔고 : 입력받은 현재잔고
		 */
	Account acc =  new Account();
	acc.showAccountInfo();
	System.out.println("------------------------");
	Account acc2 = new Account("111-1111-111");
	acc2.showAccountInfo();
	System.out.println("------------------------");
	Account acc3 = new Account("111-1111-111","홍길동");
	acc3.showAccountInfo();
	System.out.println("------------------------");
	Account acc4 = new Account("111-1111-111","홍길동",0);
	acc4.showAccountInfo();
	}
}

class Account {
	String accountNum;
	String ownerName;
	int balance;
	
	public Account() {
		accountNum = "111-1111-111";
		ownerName = "홍길동";
		balance = 0;
	}
	
	public Account(String newAccountNum) {
		accountNum = newAccountNum;
		ownerName = "홍길동";
		balance = 0;
	}
	
	public Account(String newAccountNum, String newOwnerName) {
		accountNum = newAccountNum;
		ownerName = newOwnerName;
		balance = 0;
	}
	
	public Account(String newAccountNum, String newOwnerName, int newBalance) {
		accountNum = newAccountNum;
		ownerName = newOwnerName;
		balance = newBalance;
	}
	
	public void showAccountInfo() {
		System.out.println("계좌번호 : " + accountNum);
		System.out.println("예금주명 : " + ownerName);
		System.out.println("현재잔고 : " + balance + "원");
	}
}
/*
계좌번호 : 111-1111-111
예금주명 : 홍길동
현재잔고 : 0원
------------------------
계좌번호 : 111-1111-111
예금주명 : 홍길동
현재잔고 : 0원
------------------------
계좌번호 : 111-1111-111
예금주명 : 홍길동
현재잔고 : 0원
------------------------
계좌번호 : 111-1111-111
예금주명 : 홍길동
현재잔고 : 0원
*/

 

반응형