day11_CarTest2Ex12

육희영·2021년 10월 26일
0
package com.java1.day11;

//생성자 에서 다른 생성자 호출하기
class Car1 {
	String color; // 색상
	String gearType; // 변속기 종류 - auto(자동), manual(수동)
	int door; // 문의 개수

	Car1() { // Car(String color, String gearType, int door)를 호출
		this("white", "auto", 4); // 생성자간의 호출에는 생성자의 이름 대신 this를 사용해야만 하므로
									// 'Car' 대신 'this'를 사용했다.

	}

	Car1(String color) {
		this(color, "auto", 4);
	}

	Car1(String color, String gearType, int door) {
		this.color = color; // this는 자기 자신을 의미.
		this.gearType = gearType;
		this.door = door;
	}
}

public class CarTest2Ex12 {

	public static void main(String[] args) {
		Car1 c1 = new Car1();
		Car1 c2 = new Car1("blue");

		System.out.println("c1의 color=" + c1.color + ", gearType=" + c1.gearType + ", door=" + c1.door);
		System.out.println("c2의 color=" + c2.color + ", gearType=" + c2.gearType + ", door=" + c2.door);

	}

}

출력결과

c1의 color=white, gearType=auto, door=4
c2의 color=blue, gearType=auto, door=4

0개의 댓글

관련 채용 정보