TIL 2023-10-20 Java this, this()

장규빈·2023년 10월 20일

TIL

목록 보기
7/59
post-thumbnail

this

this는 객체 클래스 내부의 생성자나 매서드에서 객체의 내부 맴버(필드 매서드)에 접근하기 위해 사용된다.(privit 변수 등...)

this()는 객체 클래스 내부의 자신의 생성자를 호출할때 사용된다

ex this, this() 사용

public class Car {

    static final String COMPANY = "GENESIS"; // 자동차 회사 : GENESIS
    String model; // 자동차 모델
    String color; // 자동차 색상
    double price; // 자동차 가격

    double speed;  // 자동차 속도 , km/h
    char gear = 'P'; // 기어의 상태, P,R,N,D
    boolean lights; // 자동차 조명의 상태


    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;
    }

    double gasPedal(double kmh, char type) {
        changeGear(type);
        speed = kmh;
        return speed;
    }

    double brakePedal() {
        speed = 0;
        return speed;
    }

    char changeGear(char type) {
        gear = type;
        return gear;
    }

    boolean onOffLights() {
        lights = !lights;
        return lights;
    }

    void horn() {
        System.out.println("빵빵");
    }

    Car returnInstance() {
        return this;
    }
}
profile
나다운사람

0개의 댓글