์๋ฐ(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 ; } } |
'Back-End > Java' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Java] ํธ๋ฆฌํ Lombok ๊ธฐ๋ฅ ์ ๋ฆฌ (0) | 2023.02.04 |
---|---|
[Java] IntelliJ์์ lombok ์ฌ์ฉํ๊ธฐ (0) | 2023.02.04 |
[Java] ํ๊ฒฝ๋ณ์ ์ค์ (0) | 2023.02.04 |
[Java] ์๋ฌ ๋ก๊ทธ ์ฐธ๊ณ ์๋ฃ (0) | 2017.11.10 |
[Java] ํ์ฌ PC ์ IP ์ฃผ์ ๋ฐ์์ค๋ ๋ก์ง (0) | 2017.02.09 |