๐ŸŒˆ 5์ฃผ์ฐจ ๊ณผ์ œ: ํด๋ž˜์Šค

glowing713ยท2020๋…„ 12์›” 18์ผ
0

live-java-study

๋ชฉ๋ก ๋ณด๊ธฐ
2/2
post-thumbnail
  • ๋ฐฑ๊ธฐ์„  ๋‹˜์ด ์œ ํŠญ์—์„œ ์ง„ํ–‰ํ•˜๋Š” JAVA Live-study ๊ณผ์ •์„ ๊ธฐ๋กํ•œ๋‹ค.
  • ๋งค ์ฃผ ์ด์Šˆ์— ์˜ฌ๋ผ์˜จ ์งˆ๋ฌธ๋“ค์— ๋Œ€ํ•ด ๊ณต๋ถ€ํ•˜๊ณ  ๋‹ต์„ ๋‚จ๊ธฐ๊ณ , ์ด์Šˆ์— ๋งํฌ๋ฅผ ๊ณต์œ ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์ง„ํ–‰๋œ๋‹ค. ์ถ”ํ›„ ๋ฐฑ๊ธฐ์„  ๋‹˜์˜ ์œ ํŠœ๋ธŒ ๋ผ์ด๋ธŒ๋ฅผ ํ†ตํ•ด ํ”ผ๋“œ๋ฐฑ์„ ๋ฐ›๊ฒŒ ๋œ๋‹ค.
  • ์กฐ๊ธˆ ๋Šฆ๋”๋ผ๋„ ๊ณผ์ œ๋ฅผ ์ˆ˜ํ–‰ํ•˜๋ฉด ์ถœ์„ ์ธ์ •์„ ํ•ด์ฃผ์‹œ๊ธฐ๋กœ ํ–ˆ๋‹ค!(๋Šฆ๋”๋ผ๋„ ๊ผญ ๊ณต๋ถ€ํ•˜๋Š”๊ฒŒ ์ค‘์š”!)

์ฆ‰, Java ๊ณต๋ถ€๋ฅผ ๊ธฐ๋กํ•˜๋Š” ๊ณผ์ •์ด๋‹ค ๐ŸŒฑ

๐Ÿ“Œ ๋ชฉํ‘œ

์ž๋ฐ”์˜ Class์— ๋Œ€ํ•ด ํ•™์Šตํ•˜์„ธ์š”.

๐Ÿ“Œ ํ•™์Šตํ•  ๊ฒƒ (ํ•„์ˆ˜)

  • ํด๋ž˜์Šค ์ •์˜ํ•˜๋Š” ๋ฐฉ๋ฒ•
  • ๊ฐ์ฒด ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ• (new ํ‚ค์›Œ๋“œ ์ดํ•ดํ•˜๊ธฐ)
  • ๋ฉ”์†Œ๋“œ ์ •์˜ํ•˜๋Š” ๋ฐฉ๋ฒ•
  • ์ƒ์„ฑ์ž ์ •์˜ํ•˜๋Š” ๋ฐฉ๋ฒ•
  • this ํ‚ค์›Œ๋“œ ์ดํ•ดํ•˜๊ธฐ

You can not write Java code without defining a class.


1. ๊ตฌ์กฐ์ฒด vs ํด๋ž˜์Šค

[Java์—๋Š” ํด๋ž˜์Šค๋งŒ ์กด์žฌํ•œ๋‹ค.]
๊ตฌ์กฐ์ฒด์™€ ํด๋ž˜์Šค๋Š” ์ƒ๋‹นํžˆ ์œ ์‚ฌํ•œ ๋ถ€๋ถ„๋“ค์ด ์žˆ์ง€๋งŒ, ๋ถ„๋ช… ๋‹ค๋ฅด๋‹ค.

C ์–ธ์–ด์˜ ๊ฒฝ์šฐ ํด๋ž˜์Šค์™€ ๊ตฌ์กฐ์ฒด๋Š” ํ™•์‹คํ•˜๊ฒŒ ๋‹ค๋ฅด๋‹ค.
  • ํด๋ž˜์Šค์—๋Š” ๋ฉ”์†Œ๋“œ(ํ•จ์ˆ˜)๋„ ํฌํ•จ๋  ์ˆ˜ ์žˆ๋‹ค.
  • ๋ฉค๋ฒ„ ๋ณ€์ˆ˜์™€ ๋ฉ”์†Œ๋“œ๋Š” public ์ด ์•„๋‹ˆ๋ผ๋ฉด ์ˆจ๊ฒจ์ ธ ์žˆ๋‹ค.
  • ์ƒ์„ฑ์ž์™€ ์†Œ๋ฉธ์ž๊ฐ€ ์กด์žฌํ•œ๋‹ค.
  • ๋ฉค๋ฒ„ ํ•จ์ˆ˜๋กœ์„œ ์—ฐ์‚ฐ์ž ์žฌ์ •์˜(overloading)๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค.
  • ์ƒ์† ๊ด€๊ณ„๊ฐ€ ์กด์žฌํ•œ๋‹ค.
  • ๊ตฌ์กฐ์ฒด์˜ ๊ฒฝ์šฐ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ๋•Œ, struct Hello hello;์™€ ๊ฐ™์€ struct ํ‚ค์›Œ๋“œ๊ฐ€ ํ•„์š”ํ•˜์ง€๋งŒ, ํด๋ž˜์Šค์˜ ๊ฒฝ์šฐ class ํ‚ค์›Œ๋“œ๋ฅผ ์ƒ๋žตํ•  ์ˆ˜ ์žˆ๋‹ค.

BUT!!!
C++ ์˜ ๊ฒฝ์šฐ ํด๋ž˜์Šค์™€ ๊ตฌ์กฐ์ฒด๋Š” ๋ช‡ ๊ฐ€์ง€์˜ ์ฐจ์ด์ ๋งŒ ์กด์žฌํ•œ๋‹ค.

์ฐจ์ด์ 
1) ํด๋ž˜์Šค์˜ ๊ธฐ๋ณธ ์ ‘๊ทผ์„ฑ์€ private ์ด๊ณ , ๊ตฌ์กฐ์ฒด๋Š” public ์ด๋‹ค.
2) class/struct๋ฅผ ์ƒ์†๋ฐ›๋Š” struct๋Š” ๋ถ€๋ชจ class/struct์— ๋Œ€ํ•ด public ํ•˜๋‹ค. class/struct๋ฅผ ์ƒ์†๋ฐ›๋Š” class๋Š” ๋ถ€๋ชจ class/struct์— ๋Œ€ํ•ด private ํ•˜๋‹ค.

1) ํด๋ž˜์Šค์˜ ๊ธฐ๋ณธ ์ ‘๊ทผ์„ฑ์€ private ์ด๊ณ , ๊ตฌ์กฐ์ฒด๋Š” public ์ด๋‹ค.

// Program 1 
#include <stdio.h> 
  
class Test { 
    int x; // x is private 
}; 
int main() 
{ 
  Test t; 
  t.x = 20; // compiler error because x is private 
  getchar(); 
  return 0; 
} 

Test ํด๋ž˜์Šค์˜ ๋ฉค๋ฒ„ ๋ณ€์ˆ˜ x๋Š” private์ด๋ฏ€๋กœ ๋ฉ”์ธ ํ•จ์ˆ˜์—์„œ ์ง์ ‘ ์ ‘๊ทผํ•˜๋Š” t.x = 20;์—์„œ ์ปดํŒŒ์ผ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

// Program 2 
#include <stdio.h> 
  
struct Test { 
    int x; // x is public 
}; 
int main() 
{ 
  Test t; 
  t.x = 20; // works fine because x is public 
  getchar(); 
  return 0; 
} 

Test ๊ตฌ์กฐ์ฒด์˜ ๋ฉค๋ฒ„ ๋ณ€์ˆ˜ x๋Š” public์ด๋ฏ€๋กœ ๋ฉ”์ธ ํ•จ์ˆ˜์—์„œ ์ง์ ‘ ์ ‘๊ทผํ•˜๋Š” t.x = 20;์ด ์ •์ƒ์ ์œผ๋กœ ์‹คํ–‰๋œ๋‹ค.


2) class/struct๋ฅผ ์ƒ์†๋ฐ›๋Š” **struct**๋Š” ๋ถ€๋ชจ class/struct์— ๋Œ€ํ•ด **public** ํ•˜๋‹ค. class/struct๋ฅผ ์ƒ์†๋ฐ›๋Š” **class**๋Š” ๋ถ€๋ชจ class/struct์— ๋Œ€ํ•ด **private** ํ•˜๋‹ค.
// Program 3 
#include <stdio.h> 
  
class Base { 
public: 
    int x; 
}; 
  
class Derived : Base { }; // is equilalent to class Derived : private Base {} 
  
int main() 
{ 
  Derived d; 
  d.x = 20; // compiler error becuase inheritance is private 
  getchar(); 
  return 0; 
} 

์ƒ์†๋ฐ›์€ ํด๋ž˜์Šค๋Š” ๋ถ€๋ชจ ํด๋ž˜์Šค์— private์ด๋ฏ€๋กœ ๋ฉค๋ฒ„ ๋ณ€์ˆ˜์— ์ง์ ‘ ์ ‘๊ทผ์ด ๋ถˆ๊ฐ€ํ•˜๋‹ค.

// Program 4 
#include <stdio.h> 
  
class Base { 
public: 
    int x; 
}; 
  
struct Derived : Base { }; // is equilalent to struct Derived : public Base {} 
  
int main() 
{ 
  Derived d; 
  d.x = 20; // works fine becuase inheritance is public 
  getchar(); 
  return 0; 
} 

์ƒ์†๋ฐ›์€ ๊ตฌ์กฐ์ฒด๋Š” ๋ถ€๋ชจ ํด๋ž˜์Šค์— public์ด๋ฏ€๋กœ ๋ฉค๋ฒ„ ๋ณ€์ˆ˜์— ์ง์ ‘ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

(์ถœ์ฒ˜: https://www.geeksforgeeks.org/structure-vs-class-in-cpp/)

2. Java ํด๋ž˜์Šค

Oracle Java Tutorial ์— ๋”ฐ๋ผ ํ•™์Šต์„ ์ง„ํ–‰ํ•จ.

1) What Is an Object?

์ด ๊ธ€์˜ ์„œ๋‘๊ฐ€ ์ƒ๋‹นํžˆ ์žฌ๋ฏธ์žˆ์—ˆ๋‹ค.

์ฃผ๋ณ€์„ ๋‘˜๋Ÿฌ๋ณด๋ฉฐ ์‚ฌ๋ฌผ(๊ฐ์ฒด)์˜ state(์ƒํƒœ)์™€ behavior(ํ–‰๋™)์„ ์‹๋ณ„ํ•ด๋ณด๋Š” ๊ฒƒ์ด ๊ฐ์ฒด์ง€ํ–ฅ์ด๋ผ๋Š” ์šฉ์–ด๋ฅผ ์ƒ๊ฐํ•ด๋ณด๋Š” ์ข‹์€ ๋ฐฉ๋ฒ•์ด๋ผ๊ณ  ์ œ์•ˆํ•œ๋‹ค.

ํƒ์ƒ ๋žจํ”„๋Š” ์ผœ์ง, ๊บผ์ง๋ผ๋Š” ๋‘ ๊ฐ€์ง€์˜ ์ƒํƒœ์™€ ์ผœ๊ธฐ, ๋„๊ธฐ๋ผ๋Š” ๋‘ ๊ฐ€์ง€์˜ ํ–‰๋™์ด ์กด์žฌํ•œ๋‹ค.
๋ผ๋””์˜ค๋Š” ์ปค์ง, ๊บผ์ง, ํ˜„์žฌ ๋ณผ๋ฅจ, ํ˜„์žฌ ๋ฐฉ์†ก๊ตญ์ด๋ผ๋Š” ์ƒํƒœ์™€ ์ผœ๊ธฐ, ๋„๊ธฐ, ๋ณผ๋ฅจ ์˜ฌ๋ฆฌ๊ธฐ, ๋ณผ๋ฅจ ์ค„์ด๊ธฐ, seek, scan, tune์ด๋ผ๋Š” ํ–‰๋™์ด ์กด์žฌํ•œ๋‹ค.
๊ฐ์ฒด๋งˆ๋‹ค ์ƒํƒœ์™€ ํ–‰๋™์˜ ์ˆ˜๋„ ๋‹ค๋ฅด๊ณ , ์‹ฌ์ง€์–ด ์–ด๋–ค ๊ฐ์ฒด๋Š” ๋‹ค๋ฅธ ๊ฐ์ฒด๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ์•„์ฑŒ ์ˆ˜๋„ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.(์ƒ์†)

๊ฐ์ฒด์˜ state๋Š” ํ•„๋“œ(๋‹ค๋ฅธ ์–ธ์–ด์—์„œ๋Š” ๋ณ€์ˆ˜๋ผ๊ณ  ๋ถ€๋ฅด๋Š”)์— ์ €์žฅ๋˜๊ณ  behavior๋Š” ๋ฉ”์†Œ๋“œ(๋‹ค๋ฅธ ์–ธ์–ด์—์„œ๋Š” ํ•จ์ˆ˜๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค)์— ๊ตฌํ˜„๋œ๋‹ค.

๋ฉ”์†Œ๋“œ๋Š” ๊ฐœ์ฒด ๋Œ€ ๊ฐœ์ฒด ํ†ต์‹ ์„ ์œ„ํ•œ ๊ธฐ๋ณธ ๋ฉ”์ปค๋‹ˆ์ฆ˜ ์—ญํ• ์„ ํ•œ๋‹ค.
๋‚ด๋ถ€ ์ƒํƒœ๋ฅผ ์ˆจ๊ธฐ๊ณ  ์˜ค๋กœ์ง€ ๋ชจ๋“  ์ƒํ˜ธ์ž‘์šฉ์ด ๊ฐ์ฒด์˜ ๋ฉ”์†Œ๋“œ๋ฅผ ํ†ตํ•ด์„œ ์ˆ˜ํ–‰๋˜๋„๋ก ์š”๊ตฌํ•˜๋Š” ๊ฒƒ์ด ๋ฐ์ดํ„ฐ ์บก์Šํ™”(Data encapsulation)์ด๋ผ๊ณ  ํ•œ๋‹ค.

2) ํด๋ž˜์Šค์˜ ์ •์˜

์œ„์—์„œ ์„ค๋ช…ํ•œ ์ž์ „๊ฑฐ๋ฅผ Class๋กœ ๊ตฌํ˜„ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

class Bicycle {

    int cadence = 0;
    int speed = 0;
    int gear = 1;

    void changeCadence(int newValue) {
        cadence = newValue;
    }

    void changeGear(int newValue) {
        gear = newValue;
    }

    void speedUp(int increment) {
        speed = speed + increment;
    }

    void applyBrakes(int decrement) {
        speed = speed - decrement;
    }

    void printStates() {
        System.out.println("cadence:" +
                cadence + " speed:" +
                speed + " gear:" + gear);
    }
}
  • ํด๋ž˜์Šค์˜ ๋ฉค๋ฒ„ ๋ณ€์ˆ˜๋ฅผ ํ•„๋“œ๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค.

    • ํ•„๋“œ์˜ ์„ ์–ธ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ˜•์‹์„ ๊ฐ€์ง„๋‹ค.
      modifier type name => public int speed

    • ์ ‘๊ทผ ์ œ์–ด์ž(Access Modifiers)๋ž€?

      • ์ ‘๊ทผ ์ œ์–ด์ž๋Š” ๋‹ค๋ฅธ ํด๋ž˜์Šค๊ฐ€ ํ•„๋“œ์— ์ ‘๊ทผํ•˜๋Š” ๊ฒƒ์„ ์ œ์–ดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
        1) public: ์–ด๋–ค ํด๋ž˜์Šค์—์„œ๋ผ๋„ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.
        2) private: ๋ฉ”์†Œ๋“œ๋Š” ํ•ด๋‹น ํด๋ž˜์Šค์—์„œ๋งŒ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.
        3) default: ์ ‘๊ทผ์ œ์–ด์ž๊ฐ€ ์—†๋Š” ๋ณ€์ˆ˜, ๋ฉ”์†Œ๋“œ๋Š” default ์ ‘๊ทผ์ œ์–ด์ž๊ฐ€ ๋˜์–ด ํ•ด๋‹น ํŒจํ‚ค์ง€ ๋‚ด์—์„œ๋งŒ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.
        4) protected: ๋™์ผ ํŒจํ‚ค์ง€๋‚ด์˜ ํด๋ž˜์Šค ๋˜๋Š” ํ•ด๋‹น ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›์€ ์™ธ๋ถ€ ํŒจํ‚ค์ง€์˜ ํด๋ž˜์Šค์—์„œ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

3) ๋ฉ”์†Œ๋“œ์˜ ์ •์˜

๋ฉ”์†Œ๋“œ์˜ ์„ ์–ธ์€ ๋‹ค์Œ์˜ ํ•ญ๋ชฉ๋“ค์„ ํฌํ•จํ•œ๋‹ค.
1) ์ ‘๊ทผ ์ œ์–ด์ž
2) ๋ฆฌํ„ด ํƒ€์ž…
3) ๋ฉ”์†Œ๋“œ ์ด๋ฆ„ - ๋„ค์ด๋ฐ ์ปจ๋ฒค์…˜์„ ๋”ฐ๋ฅธ๋‹ค. camelCase.
4) ํŒŒ๋ผ๋ฏธํ„ฐ
5) ์˜ˆ์™ธ ์ฒ˜๋ฆฌ - ์ดํ›„ ๋‹ค๋ฃฐ ๋‚ด์šฉ
6) brace๋กœ ๋‹ซํžŒ ๋ฉ”์†Œ๋“œ์˜ ๋‚ด์šฉ

void changeCadence(int newValue) {
        cadence = newValue;
}

4) ์ƒ์„ฑ์ž์˜ ์ •์˜

์ƒ์„ฑ์ž๋Š” blueprint๋กœ๋งŒ ์กด์žฌํ–ˆ๋˜ ํด๋ž˜์Šค์˜ ์‹ค์ œ object๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ํ˜ธ์ถœ๋œ๋‹ค.
๋ฉ”์†Œ๋“œ์˜ ์ •์˜์™€ ๋น„์Šทํ•˜๊ฒŒ ์ƒ๊ฒผ์ง€๋งŒ, ์ƒ์„ฑ์ž์˜ ์ด๋ฆ„์œผ๋กœ ํด๋ž˜์Šค์˜ ์ด๋ฆ„์„ ์‚ฌ์šฉํ•œ๋‹ค๋Š” ์ ์ด ๋‹ค๋ฅด๋‹ค.

์œ„์—์„œ ์ •์˜ํ•œ ์ž์ „๊ฑฐ ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž์ด๋‹ค.

public Bicycle(int startCadence, int startSpeed, int startGear) {
    gear = startGear;
    cadence = startCadence;
    speed = startSpeed;
}

์ด์ œ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•œ ์ƒ์„ฑ์ž๋ฅผ ์ •์˜ํ–ˆ์œผ๋‹ˆ, ์‹ค์ œ ์ž์ „๊ฑฐ ๊ฐ์ฒด๋ฅผ myBike๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ ์ƒ์„ฑํ•ด๋ณด์ž.

Bicycle myBike = new Bicycle(30, 0, 8);

์—ฌ๊ธฐ์„œ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•ด new ๋ผ๋Š” ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค.
new Bicycle(30, 0, 8) ์€ myBike๋ผ๋Š” ๋ณ€์ˆ˜์˜ ์‹ค์ œ ๊ณต๊ฐ„์„ Heap ์˜์—ญ์— ํ• ๋‹นํ•˜๊ณ , ๊ทธ ์ฃผ์†Œ๊ฐ’์„ ๋ฉ”๋ชจ๋ฆฌ์˜ Stack ์˜์—ญ์— ์ €์žฅํ•œ๋‹ค.

์ƒ์„ฑ์ž๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ์˜ ๊ฐœ์ˆ˜์— ๋”ฐ๋ผ ์—ฌ๋Ÿฌ ๊ฐœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.

public Bicycle(int startCadence, int startSpeed, int startGear) {
    this.cadence = startCadence;
    this.speed = startSpeed;
    this.gear = startGear;
}

public Bicycle(int startCadence, int startSpeed) {
    this.cadence = startCadence;
    this.speed = startSpeed;
}

public Bicycle(int startCadence) {
    this.cadence = startCadence;
}

public Bicycle() {
    this.cadence = 10;
    this.speed = 0;
    this.gear = 1;
}
  • ํŒŒ๋ผ๋ฏธํ„ฐ ์ˆ˜๊ฐ€ ๊ฐ™์€ ์ƒ์„ฑ์ž๋ฅผ ์—ฌ๋Ÿฌ ๊ฐœ ๋งŒ๋“ค ์ˆ˜ ์—†๋‹ค.
  • ํด๋ž˜์Šค ๋‚ด๋ถ€์— ์ƒ์„ฑ์ž ์„ ์–ธ์„ ์ƒ๋žตํ–ˆ๋‹ค๋ฉด, ์ปดํŒŒ์ผ๋Ÿฌ๋Š” ๊ธฐ๋ณธ ์ƒ์„ฑ์ž๋ฅผ ์ถ”๊ฐ€ํ•ด์ค€๋‹ค.
  • ์ƒ์„ฑ์ž๋ฅผ ํ•˜๋‚˜๋ผ๋„ ์ง์ ‘ ์„ ์–ธํ–ˆ๋‹ค๋ฉด, ๋ชจ๋“  ํ•„์š”ํ•œ ์ƒ์„ฑ์ž๋ฅผ ์ง์ ‘ ์„ ์–ธํ•ด์•ผ ํ•œ๋‹ค.

0๊ฐœ์˜ ๋Œ“๊ธ€