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 ๋ผ์ธ์€ ์ง€์šฐ์ง€ ๋ง ๊ฒƒ

Button.java
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 : ๊ฐ ๋ฒ„ํŠผ์ด ๊ฐ–๊ณ  ์žˆ์„ ์†์„ฑ(๋“ค)๊ณผ ๊ธฐ๋Šฅ(๋“ค)์„ ์ •์˜

Main.java
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

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

Main.java
//  ๋ณธ์‚ฌ ์†Œ์†์˜ ๋งค์žฅ์„ ๋‚ด๋Š” ์ฝ”๋“œ
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

DancingCup.java
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 - ๋งŒ๋“ค์–ด์งˆ ์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ€๋ฆฌํ‚ด

  • intro ๋ฉ”์†Œ๋“œ ๋ธŒ๋ ˆ์ดํฌํฌ์ธํŠธ์—์„œ this ํ•ญ๋ชฉ ํ™•์ธ

    • no ์™€ name ์— this ๋ฅผ ๋ถ™์ธ ๊ฒƒ๊ณผ ๊ฐ™์Œ

  • ๋ฉ”์†Œ๋“œ ๋‚ด์—์„œ ๊ฐ™์€ ์ด๋ฆ„์˜ ๋ณ€์ˆ˜๋‚˜ ์ธ์ž๊ฐ€ ์—†๋‹ค๋ฉด ์‹๋ณ„์ž๋Š” this ์˜ ํ•„๋“œ๋ฅผ ๊ฐ€๋ฆฌํ‚ด

  • ๊ฐ™์€ ์ด๋ฆ„์˜ ๋ณ€์ˆ˜๋‚˜ ์ธ์ž๊ฐ€ ์žˆ๋‹ค๋ฉด ๋ฎ์–ด์”Œ์›Œ์ง

    • ํ•„๋“œ์—๋Š” this๋ฅผ ๋ถ™์—ฌ ๊ตฌ๋ถ„

    • ์ƒ์„ฑ์ž์—์„œ this ๋ฅผ ๋นผ๊ณ  ์‹คํ–‰ํ•ด ๋ณผ ๊ฒƒ

    • intro ์—์„œ ์ฃผ์„ ํ•ด์ œํ•ด ๋ณผ ๊ฒƒ, this ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋‹ค์‹œ ํ•ด ๋ณผ ๊ฒƒ

Last updated