Class
๊ฐ์ฒด์งํฅ์ ์ง์
์ฅ๋ฒฝ์ด ์๋ ์ฃผ์
๋ฐ๋ก ์ค๋ช ํ๊ธฐ๋ณด๋ค, ์ง์ ์ค์ต์ ํตํด ํ๋ํ๋ ์์๊ฐ ๊ฒ
ํด๋์ค/๊ฐ์ฒด ์์ด ํ๋ก๊ทธ๋๋ฐ์ ํ๋ค๋ฉด?
// 1 ๋ฒํผ
char btn1Print = '1';
int btn1Space = 1;
String btn1Mode = "DARK";
placeButton(btn1Print, btn1Space, btn1Mode);
// ๋ํ๊ธฐ ๋ฒํผ
char btnPlusPrint = '+';
int btnPlusSpace = 3;
String btnPlusMode = "DARK";
placeButton(btnPlusPrint, btnPlusSpace, btnPlusMode);
// ํด๋ฆฌ์ด ๋ฒํผ
char btnClearPrint = 'C';
int btnClearSpace = 2;
String btnClearMode = "DARK";
placeButton(btnClearPrint, btnClearSpace, btnClearMode);
static void placeButton (char print, int space, String mode) {
System.out.printf(
"ํ์: %c, ๊ณต๊ฐ: %s, ๋ชจ๋: %s%n",
print, space, mode
);
}
๊ฐ์/์ ์ฌํ ํ์์ ๋ฐ๋ณต๋๋ ์ฝ๋๋ค
๋ฐ๋ณต์ ์ค์ด๊ณ , ์ฒด๊ณ์ ์ด๊ณ ์์ ์ ์ด๊ฒ ์ด ๋ฒํผ๋ค์ ๋ค๋ฃฐ ๋ฐฉ๋ฒ ํ์
Ex01 - button.java
ํ์ผ์์ฑ์ ์์ฑ๋ ์ต์๋จ์ package ๋ผ์ธ์ ์ง์ฐ์ง ๋ง ๊ฒ
public class Button {
char print;
int space;
String mode;
Button (char print, int space, String mode) {
this.print = print;
this.space = space;
this.mode = mode;
}
void place () {
System.out.printf(
"ํ์: %c, ๊ณต๊ฐ: %s, ๋ชจ๋: %s%n",
print,space, mode
);
}
}
[Main.java](<http://Main.java>)
ํ์ผ๊ณผ ๊ฐ์ ์์น์ ์์ฑํด๋์ค class : ๊ฐ ๋ฒํผ์ด ๊ฐ๊ณ ์์ ์์ฑ(๋ค)๊ณผ ๊ธฐ๋ฅ(๋ค)์ ์ ์
Button button1 = new Button('1', 1, "DARK");
Button buttonPlus = new Button('+', 3, "DARK");
Button buttonClear = new Button('C', 2, "DARK");
button1.place();
buttonPlus.place();
buttonClear.place();
๊ฐ์ฒด object / ์ธ์คํด์ค instance : ์์ฑ(ํ๋กํผํฐ)๋ค๊ณผ ๊ธฐ๋ฅ(๋ฉ์๋)๋ค์ ๋ฌถ์
์๋ฐ์์๋ ๊ฐ์ฒด์ ์ธ์คํด์ค๋ฅผ ๊ฐ์ ๊ฒ์ผ๋ก ์ดํดํด๋ ๋จ
์ธ์คํด์ค๋ ํด๋์ค์์ ์ ์ํ ๋ฐฉ์์ผ๋ก ์์ฐ๋จ
๐ก ํด๋์ค & ์ธ์คํด์ค - ํ๋์ฐจ์ด์ฆ ๋ณธ์ฌ & ๋งค์ฅ
์ด๋ฐ๋ถ์ ์ดํด๋ฅผ ์ํด ํ๋ ์ฐจ์ด์ฆ ๋น์ ๋ฅผ ์ฌ์ฉํ ๊ฒ
ex02 - DancingCup.java
// ๋ณธ์ฌ์ ์ฝ๋
public class DancingCup{
// ์ธ์คํด์ค๊ฐ ๊ฐ์ง ํ๋(field)๋ค
int no;
String name;
// ์ธ์คํด์ค๊ฐ ๊ฐ์ง ๋ฉ์๋ - ๐ก static์ ๋ถ์ด์ง ์์
String intro () {
// no์ name ์์ this๋ฅผ ๋ถ์ธ ๊ฒ๊ณผ ๊ฐ์
return "์๋
ํ์ธ์, %dํธ %s์ ์
๋๋ค."
.formatted(no, name);
}
}
โญ ํด๋์คํ์ผ์ ์ด๋ฆ์ ํด๋์ค์ ์ด๋ฆ๊ณผ ๋์ผํด์ผ ํจ (
public
ํด๋์ค์ผ ์)IntelliJ์์ ํด๋์ค๋ช ์์ ํด ๋ณผ ๊ฒ - refactor
// ๋ณธ์ฌ ์์์ ๋งค์ฅ์ ๋ด๋ ์ฝ๋
DancingCupstore1 = new DancingCup();
store1.no = 1; // ๐ด
store1.name = "์๋ฉด";
DancingCupstore2 = new DancingCup();
store2.no = 2;
store2.name = "๋จํฌ";
// ์ธ์คํด์ค์ ํ๋๋ค์ ์ ๊ทผ
int store1No = store1.no;
String store2Name = store2.name;
// ์ธ์คํด์ค์ ๋ฉ์๋ ํธ์ถ
String store1Intro = store1.intro();
๋๋ฒ๊น ํด๋ก ์ธ์คํด์ค๋ค ์ดํด๋ณผ ๊ฒ
out
๋๋ ํ ๋ฆฌ ์ดํด๋ณผ ๊ฒ - ํด๋์ค๋ค์ด.class
ํ์ผ๋ก ์์ฑ
โญ๏ธ ์์ฑ์ ๋ฉ์๋ - ๋งค์ฅ์ ๋ด๋ ๋ฉ์๋
ex03
public class DancingCup{
int no;
String name;
// โญ ์์ฑ์(constructor) : ์ธ์คํด์ค๋ฅผ ๋ง๋๋ ๋ฉ์๋
// โญ this : ์์ฑ๋ ์ธ์คํด์ค๋ฅผ ๊ฐ๋ฆฌํด
DancingCup(int no, String name) {
this.no = no;
this.name = name;
}
String intro () {
// String name = "๋ชฝ๊ณ ๋ฐ"; // ์ฃผ์ํด์ ์ name ๋์ฒด
return "์๋
ํ์ธ์, %dํธ %s์ ์
๋๋ค." // ๐ด
.formatted(no, name);
}
}
// ํด๋์ค๋ก ์ธ์คํด์ค๋ฅผ ์์ฑ - ๐ก new ์ฐ์ฐ์ + ์์ฑ์ ํธ์ถ
// ๋ณธ์ฌ์ ๋ฐฉ์นจ๋๋ก ๋งค์ฅ์ ๋ด๋ ๊ฒ
DancingCupstore1 = new DancingCup(1, "์๋ฉด");
DancingCupstore2 = new DancingCup(2, "๋จํฌ");
DancingCupstore3 = new DancingCup(3, "๋ถ์ฐ๋");
String[] intros = {
store1.intro(), store2.intro(), store3.intro()
};
๋ฉ์๋ ์ด๋ฆ ์์ด, ๋ฐํ ํ์ (ํด๋น ํด๋์ค) ๋ค๋ก ๊ดํธ๊ฐ ๋ฐ๋ผ์ด
return
์ ๋ช ์ํ์ง ์์ - ํด๋น ํด๋์ค ํ์ ์ ์ธ์คํด์ค ๋ฐํnew
์ฐ์ฐ์์ ํจ๊ป ์ฌ์ฉ๋์ด ์ธ์คํด์ค๋ฅผ ๋ฐํํ์ ์์ฑ ์๋ - ์ธ์ ๋ ๊ฐ์ ๋ด์ฉ์ ์ธ์คํด์ค๋ฅผ ๋ฐํํ ๊ฒฝ์ฐ
์์ฑ๋์ง ์์์ ๊ฒฝ์ฐ์๋ ์ธ์ ์์ด ํธ์ถ (์ด์ ์์ ํ์ธ)
โญ๏ธ ์ฝ๋์ ์์ฑํ์ง ์์๋ ์ปดํ์ผ๋ฌ๊ฐ ์๋ ์์ฑ
.class
ํ์ผ์์ ํ์ธ ๊ฐ๋ฅ
์๋์์ฑ
๋ฉ๋ด - ์ฝ๋ - ์์ฑ
์๋์ฐ:
alt
+insert
๋งฅ:
command
+N
์์ฑ์
์ ํname
๊ณผprice
์ ํ
๐ก ์์ฑ์๋ฅผ ์์ฑํ์ง ์๋๋ค๊ณ ์์ฑ์๊ฐ ์๋ ๊ฒ์ด ์๋
ex02
์ ์ปดํ์ผ๋ ํ์ผ DancingCup.class
์ดํด ๋ณผ ๊ฒ
โญ๏ธ this
- ๋ง๋ค์ด์ง ์ธ์คํด์ค๋ฅผ ๊ฐ๋ฆฌํด
this
- ๋ง๋ค์ด์ง ์ธ์คํด์ค๋ฅผ ๊ฐ๋ฆฌํดintro
๋ฉ์๋ ๋ธ๋ ์ดํฌํฌ์ธํธ์์this
ํญ๋ชฉ ํ์ธno
์name
์this
๋ฅผ ๋ถ์ธ ๊ฒ๊ณผ ๊ฐ์
๋ฉ์๋ ๋ด์์ ๊ฐ์ ์ด๋ฆ์ ๋ณ์๋ ์ธ์๊ฐ ์๋ค๋ฉด ์๋ณ์๋
this
์ ํ๋๋ฅผ ๊ฐ๋ฆฌํด๊ฐ์ ์ด๋ฆ์ ๋ณ์๋ ์ธ์๊ฐ ์๋ค๋ฉด ๋ฎ์ด์์์ง
ํ๋์๋
this
๋ฅผ ๋ถ์ฌ ๊ตฌ๋ถ์์ฑ์์์
this
๋ฅผ ๋นผ๊ณ ์คํํด ๋ณผ ๊ฒintro
์์ ์ฃผ์ ํด์ ํด ๋ณผ ๊ฒ,this
๋ฅผ ์ฌ์ฉํ์ฌ ๋ค์ ํด ๋ณผ ๊ฒ
Last updated