this
this
: κ°μ²΄(μΈμ€ν΄μ€) μμ μ νννλ β‘οΈ ν€μλ μκΈ° μμ μ κ°λ¦¬ν¨λ€ !!
public class Car {
String model;
String color;
double price;
public Car(String model, String color, double price) {
model = model;
color = color;
price = price;
}
}
μ΄λ κ² μμ±μλ₯Ό μ μΈνλ€λ©΄ 맀κ°λ³μλͺ κ³Ό κ°μ²΄μ νλλͺ μ΄ λμΌν κ²½μ° λ¬Έλ²μ λ¬Έμ λ μκΈ° λλ¬Έμ μ€λ₯κ° λ°μνμ§λ μλλ€
λλ 맨 μμ μλ
model
,color
,price
μ ν λΉνκ³ μΆμ΄
νμ§λ§ !!
μμ±μ λΈλ‘ λ΄λΆμμ ν΄λΉ λ³μλ€μ κ°μ²΄μ νλκ° μλ κ°μ₯ κ°κΉμ΄ 맀κ°λ³μλͺ
μ κ°λ¦¬ν€κ² λ¨μΌλ‘ μκΈ° μμ μκ² κ°μ λμ
νλ μν©μ΄ λμ΄λ²λ¦°λ€ !!
μ΄λ° 방ꡬκ°μ μν©μ this
ν€μλλ₯Ό μ¬μ©νμ¬ ν΄κ²°ν μ μλ€
public class Car {
String model;
String color;
double price;
public Car(String model, String color, double price) {
this.model = model;
this.color = color;
this.price = price;
}
}
π€π» μμΌλ‘ μμ±μλ₯Ό μΈ λλ 무쑰건
this
λ₯Ό μ¬μ©νμ ! μ½μν΄ !
this
λ μΈμ€ν΄μ€ μμ μ λ»νκΈ° λλ¬Έμ κ°μ²΄μ λ©μλμμ μΈμ€ν΄μ€ μμ μ ν΄λμ€ νμ
μ 리ν΄νκ³ μΆλ€λ©΄ this
λ₯Ό μ¬μ©νμ¬ μΈμ€ν΄μ€ μμ μ μ£Όμλ₯Ό λ°νν μλ μλ€ !!
Car returnInstance() { // 리ν΄νμ
λ©μλμ΄λ¦()
return this; // return Car
} // Car = this
this()
μΈμ€ν΄μ€ μμ μ μμ±μλ₯Ό νΈμΆνλ ν€μλ
class Car() {
Car() {
}
} // μ¬κΈ°μ thisλ Carμ΄λ€
|---------------------------------------------------------|
class this() { // Car = this
this() { // μμ±μ
}
} // this()λ μμ±μλ λ»νλ€
// # μ€λ³΅
public Car(String model) {
this.model = model;
this.color = "Blue";
this.price = 50000000;
}
public Car(String model, String color) {
this.model = model;
this.color = color;
this.price = 50000000;
}
public Car(String model, String color, double price) {
this.model = model;
this.color = color;
this.price = price;
}
|---------------------------------------------------------|
// # μ€λ³΅ μ κ±°
public Car(String model) { // μμ±μ #1
this(model, "Blue", 50000000);
}
public Car(String model, String color) { // μμ±μ #2
this(model, color, 100000000);
}
public Car(String model, String color, double price) { // μμ±μ #3
this.model = model;
this.color = color;
this.price = price;
}
μ½λμ μ€λ³΅μ΄ μμ λ this()
μ¬μ©ν΄ μ½λμ μ€λ³΅μ μ κ±°νλ€.
μκΈ° μμ μμ±μλ₯Ό νΈμΆνλ©΄μ 맀κ°λ³μ 3κ°μΈ κ²μ νΈμΆνλ€ β‘οΈ λ¬΄μ‘°κ±΄ μμ±μ #3
μΌλ‘ νκ·νλ€ !!
π¨ μ£Ό μ π¨
this()
ν€μλλ₯Ό μ¬μ©ν΄μ λ€λ₯Έ μμ±μλ₯Ό νΈμΆν λλ λ°λμ ν΄λΉ μμ±μμ 첫 μ€μ μμ±μ΄ λμ΄μΌνλ€.public Car(String model) { System.out.println("model = " + model); this(model, "Blue", 50000000); }
this()
μμ λ€λ₯Έ μ½λκ° μμΌλ―λ‘ μ€λ₯ λ°μ !