[Java] this, this( )

CountryGirlΒ·2023λ…„ 5μ›” 31일
0

Java

λͺ©λ‘ 보기
7/18
post-thumbnail

πŸ“Œ 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() μœ„μ— λ‹€λ₯Έ μ½”λ“œκ°€ μžˆμœΌλ―€λ‘œ 였λ₯˜ λ°œμƒ !

profile
πŸ’»πŸŒΎμ‹œκ³¨μ†Œλ…€μ˜ 엉망징창 개발 μ„±μž₯μΌμ§€πŸŒΎπŸ’» (2023.05.23 ~)

0개의 λŒ“κΈ€

κ΄€λ ¨ μ±„μš© 정보