생성자 this()
- 생성자에서 다른 생성자 호출할 때 사용(코드 중복제거를 위해 다른 생성자 호출을 함)
- 다른 생성자 호출시 첫 줄에서만 사용가능
class Car2 {
String color;
String gearType;
int door;
Car2(){
this("white", "auto", 4);
}
Car2(String color){
this(color, "auto", 4);
}
Car2(String color, String gearType, int door){
this.color = color;
this.gearType = gearType;
this.door = door;
}
}
참조변수 this
- 인스턴스 자신을 가리키는 참조변수
- 인스턴스 메서드(생성자 포함)에서 사용 가능
- 지역변수(lv)와 인스턴스 변수(iv)를 구별할 때 사용
class Car {
String color;
String gearType;
int door;
Car(String color, String gearType, int door){
this.color = color;
this.gearType = gearType;
this.door = door;
}
}