this ์ฐธ์กฐ ๋ณ์
this ์ฐธ์กฐ ๋ณ์๋ ์ธ์คํด์ค๊ฐ ๋ฐ๋ก ์๊ธฐ ์์ ์ ์ฐธ์กฐํ๋๋ฐ ์ฌ์ฉํ๋ ๋ณ์..
๋ฐ๋ผ์ this ์ฐธ์กฐ ๋ณ์๋ ํด๋น ์ธ์คํด์ค์ ์ฃผ์๋ฅผ ๊ฐ๋ฆฌํด.
* ์์ฑ์์ ๋งค๊ฐ๋ณ์ ์ด๋ฆ๊ณผ ์ธ์คํด์ค ๋ณ์์ ์ด๋ฆ์ด ๊ฐ์ ๋ ์ธ์คํด์ค ๋ณ์ ์์ this.์ ๋ถ์ฌ์ ๊ตฌ๋ถ
(this ์๋ถ์ด๋ฉด ์ปดํ์ผ๋ฌ๊ฐ ๊ตฌ๋ถ ๋ชป ํจ)
๋ชจ๋ ์ธ์คํด์ค ๋ฉ์๋์๋ this ์ฐธ์กฐ ๋ณ์๊ฐ ์จ๊ฒจ์ง ์ง์ญ๋ณ์๋ก ์กด์ฌํจ!
this() ๋ฉ์๋ - ๋ค๋ฅธ ์์ฑ์ ํธ์ถ
์์ฑ์ ์ค๋ฒ๋ก๋ฉ์ด ๋ง์์ง ๊ฒฝ์ฐ ์์ฑ์ ๊ฐ์ ์ค๋ณต์ฝ๋๊ฐ ๋ฐ์ํ ์ ์์.
this() ๋ฉ์๋๋ ์์ฑ์ ๋ด๋ถ์์๋ง ์ฌ์ฉ ๊ฐ๋ฅํ๋ฉฐ, ๊ฐ์ ํด๋์ค์ ๋ค๋ฅธ ์์ฑ์๋ฅผ ํธ์ถํ ๋ ์ฌ์ฉํจ.
this() ๋ฉ์๋์ ์ธ์๋ฅผ ์ ๋ฌํ๋ฉด, ์ผ์นํ๋ ์์ฑ์๋ฅผ ์ฐพ์์ ํธ์ถํด ์ค!!
class Car {
private String modelName;
private int modelYear;
private String color;
private int maxSpeed;
public Car(String modelName, int modelYear, String color, int maxSpeed) {
this.modelName = modelName;
this.modelYear = modelYear;
this.color = color;
this.maxSpeed = maxSpeed;
}
public Car(String modelName, int modelYear, String color) {
this.modelName = modelName;
this.modelYear = modelYear;
this.color = color;
}
public Car() {
this("์ผํํ",2022, "Black");
}
public Car(String model) {
this(model, 2022, "red", 220);
}
}