[Java] this()

Sun choi·2024년 7월 25일

NEW 지식

목록 보기
10/34

this() 키워드를 사용해 코드의 중복을 제거

public Car(String model) {
    this(model, "Blue", 50000000);
}

public Car(String model, String color) {
    this(model, color, 100000000);
}

public Car(String model, String color, double price) {
    this.model = model;
    this.color = color;
    this.price = price;
}

this() 키워드를 사용해서 다른 생성자를 호출할 때는 반드시 해당 생성자의 첫 줄에 작성,

 public Car(String model) {
        this(model, "Blue", 50000000);
    }

    public Car(String model, String color) {
        this(model, color, 100000000);
    }

    public Car(String model, String color, double price) {
        this.model = model;
        this.color = color;
        this.price = price;
    }

super과 super() 비슷함 근데 부모클래스 호출하는 것.

profile
풀스택 개발자의 공부기록 📖

0개의 댓글