μƒˆμ†Œμ‹

Back-End/Java

[Java] this, super ν‚€μ›Œλ“œ, this(), super() ν•¨μˆ˜ μ‚¬μš©ν•˜λŠ” 방법

  • -

 

 

 

 

μžλ°”(Java) this, super ν‚€μ›Œλ“œ, this(), super() ν•¨μˆ˜ μ‚¬μš©ν•˜λŠ” 방법

 

 

 

ν™˜κ²½: Eclipse Mars

 

 

thisλŠ” ν˜„μž¬ 클래슀의 μΈμŠ€ν„΄μŠ€λ₯Ό κ°€λ¦¬ν‚΅λ‹ˆλ‹€. super λŠ” λΆ€λͺ¨ 클래슀λ₯Ό κ°€λ¦¬ν‚΅λ‹ˆλ‹€ν˜„μž¬ 클래슀의 μΈμŠ€ν„΄μŠ€μ— μžˆλŠ” μ†μ„±μ΄λ‚˜ ν•¨μˆ˜λ₯Ό μ œμ–΄ν•˜λ €λ©΄ this.setName() μ„ ν•˜κ³  λΆ€λͺ¨ 클래슀의 ν•¨μˆ˜λ₯Ό ν˜ΈμΆœν•˜κ³  μ‹ΆμœΌλ©΄ super.setName() μ„ μž…λ ₯ν•©λ‹ˆλ‹€μ•„λž˜ μ†ŒμŠ€λ₯Ό λ³΄λ©΄μ„œ μ–΄λ–»κ²Œ μ‚¬μš©ν•˜λŠ”μ§€ μ•Œμ•„ 보도둝 ν•˜κ² μŠ΅λ‹ˆλ‹€.

 

 

¤
 ν΄λž˜μŠ€μ˜ 속성과 λ§€κ°œλ³€μˆ˜μ˜ 이름이 같을 λ•Œ

 

 β–Ό μ•„λž˜ μ†ŒμŠ€λŠ” this λ₯Ό μ‚¬μš©ν•˜μ§€ μ•Šμ•˜μ„ λ•Œ μƒν™©μž…λ‹ˆλ‹€μ£Όλ‘œ set/get ν•¨μˆ˜λ₯Ό λ§Œλ“€ λ•Œ μ‹€μˆ˜ν•˜λŠ” 경우 μž…λ‹ˆλ‹€λ§€κ°œ λ³€μˆ˜μ˜ 이름과 Fruit μ— 내에 μ§€μ—­λ³€μˆ˜ 이름이 κ°™κΈ° λ•Œλ¬Έμ— Fruit κ°μ²΄μ˜ name μ†μ„±μ— 값이 μ €μž₯λ˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€μ΄ λ•Œ Fruit μΈμŠ€ν„΄μŠ€λ₯Ό κ°€λ¦¬ν‚€λŠ” this ν‚€μ›Œλ“œλ₯Ό μ‚¬μš©ν•΄μ•Ό λ˜λŠ” κ²ƒμž…λ‹ˆλ‹€.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class Fruit {
    public String name;
    public String color;
    public double weight;
         
    public Fruit(String name, String color, double weight) {
        name = name;
        color = color;
        weight = weight;
    }
     
    public static void main(String[] args) {
        Fruit banana = new Fruit("banana", "yellow", 5.0);
        System.out.println("name : " + banana.name);   
        System.out.println("color : " + banana.color); 
        System.out.println("weight : " + banana.weight);
    }
}

 

β–Ό μ•„λž˜ 그림처럼 λ§€κ°œλ³€μˆ˜λ‘œ λ„˜μ–΄μ˜¨ 값을 Fruit μ†μ„±μ— μ €μž₯ν•˜λ €λ©΄ λͺ¨λ“  속성에 this λ₯Ό λΆ™μ—¬ μ€λ‹ˆλ‹€.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class Fruit {
    public String name;
    public String color;
    public double weight;
         
    public Fruit(String name, String color, double weight) {
        this.name = name;
        this.color = color;
        this.weight = weight;
    }
     
    public static void main(String[] args) {
        Fruit banana = new Fruit("banana", "yellow", 5.0);
        System.out.println("name : " + banana.name);   
        System.out.println("color : " + banana.color); 
        System.out.println("weight : " + banana.weight);
    }
}

 

 

 
¤
 μ˜€λ²„λ‘œλ”©λœ λ‹€λ₯Έ μƒμ„±μž ν˜ΈμΆœν•˜λŠ” 방법

 

 β–Ό this() λŠ” μžκΈ°μžμ‹ μ˜ μƒμ„±μžλ₯Ό ν˜ΈμΆœν•¨μœΌλ‘œμ„œ μƒμ„±μžμ˜ μ΄ˆκΈ°ν™” 과정을 λ°˜λ³΅ν•˜μ§€ μ•Šμ•„λ„ λ©λ‹ˆλ‹€. this(x, y) μ²˜λŸΌ νŒŒλΌλ―Έν„°κ°€ 있으면 자기 μžμ‹ μ˜ ν΄λž˜μŠ€μ— ν•΄λ‹Ήν•˜λŠ” λ™μΌν•œ νŒŒλΌλ―Έν„° 객체λ₯Ό 가진 ν•¨μˆ˜λ₯Ό ν˜ΈμΆœν•˜κ²Œ λ©λ‹ˆλ‹€.super() λŠ” 상속받은 λ°”λ‘œ μœ„ 클래슀의 μƒμ„±μžλ₯Ό ν˜ΈμΆœν•˜κ²Œ λ©λ‹ˆλ‹€. super(x, y) λŠ” this μ˜ νŒŒλΌλ―Έν„° λ„˜κΈ°λŠ” ν•¨μˆ˜μ²˜λŸΌ 상속받은 ν΄λž˜μŠ€μ—μ„œ λ™μΌν•œ νŒŒλΌλ―Έν„° 객체λ₯Ό 가진 ν•¨μˆ˜λ₯Ό ν˜ΈμΆœν•©λ‹ˆλ‹€μ΄κ²ƒμ΄ this μ™€ super μ˜ μ°¨μ΄μ μž…λ‹ˆλ‹€μ£Όμ˜ 할점은 this() μ™€ super() λŠ” { λ°”λ‘œ λ‹€μŒμ—λ§Œ μ“Έ 수 μžˆμŠ΅λ‹ˆλ‹€κ·Έλž˜μ„œ this(), super() λŠ” 같이 μ“Έ μˆ˜κ°€ μ—†λŠ” κ²ƒμž…λ‹ˆλ‹€.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
class UpperClass {
    int x;
    int y;
 
    public UpperClass() {
        x = 10;
        y = 20;
    }
 
    public UpperClass(int x) {
        this(); // μžμ‹ μ˜ 클래슀 public UpperClass() μƒμ„±μž 호좜
        this.x = x;
    }
 
    public UpperClass(int x, int y) {
        this(x); // μžμ‹ μ˜ 클래슀 public UpperClass(int x) μƒμ„±μž 호좜
        this.y = y;
    }
}
 
class LowerClass extends UpperClass {
    int r;
 
    public LowerClass() {
        super(); // μƒμœ„ 클래슀의 public UpperClass() μƒμ„±μž 호좜.
        r = 30;
    }
    public LowerClass(int x) {
        super(x); // μƒμœ„ 클래슀의 public UpperClass(int x) μƒμ„±μž 호좜
        r = 30;
    }
 
    public LowerClass(int x, int y) {
        super(x, y); // μƒμœ„ 클래슀의 public UpperClass(int x, int y) 호좜
        r = 30;
    }
 
    public LowerClass(int x, int y, int r) {
        this(x, y); // μžμ‹ μ˜ 클래슀 public LowerClass(int x, int y) 호좜
        this.r = r;
    }
}
 
public class Exe{
   public static void main(String[] ar) {
 
   }
}

 

 

¤
 μžμ‹ μ˜ 참쑰값을 μ „λ‹¬ν•˜κ³  싢을 λ•Œ

 

 β–Ό μ–΄λ–€ ν•¨μˆ˜μ—μ„œλŠ” λ¦¬ν„΄κ°’μœΌλ‘œ 자기 μžμ‹ μ˜ 참쑰값을 μ „λ‹¬ν•˜κ³  싢을 λ•Œκ°€ μžˆμŠ΅λ‹ˆλ‹€μ΄ λ•Œ this ν‚€μ›Œλ“œλ₯Ό μ΄μš©ν•¨μœΌλ‘œμ¨ 해결이 κ°€λŠ₯ν•©λ‹ˆλ‹€.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class Fruit {
    public String name;
    public String color;
    public double weight;
         
    public Fruit(String name, String color, double weight) {
        this.name = name;
        this.color = color;
        this.weight = weight;
    }
         
    public Fruit getInstance() {
        return this;
    }
}

 

Contents

ν¬μŠ€νŒ… μ£Όμ†Œλ₯Ό λ³΅μ‚¬ν–ˆμŠ΅λ‹ˆλ‹€. 😊

이 글이 도움이 λ˜μ—ˆλ‹€λ©΄ 곡감 λΆ€νƒλ“œλ¦½λ‹ˆλ‹€. πŸ‘