[Java] this, super ํ‚ค์›Œ๋“œ, this(), super() ํ•จ์ˆ˜ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•

 

 

 

 

์ถœ์ฒ˜ : http://mainia.tistory.com/85
์ž๋ฐ”(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;
    }
}