Home > CS > 2024 > πŸ’Ύ [CS] 객체 지ν–₯ ν”„λ‘œκ·Έλž˜λ°μ—μ„œμ˜ 객체(Object)λž€ λ¬΄μ—‡μΌκΉŒμš”?

πŸ’Ύ [CS] 객체 지ν–₯ ν”„λ‘œκ·Έλž˜λ°μ—μ„œμ˜ 객체(Object)λž€ λ¬΄μ—‡μΌκΉŒμš”?
CS

πŸ’Ύ [CS] 객체 지ν–₯ ν”„λ‘œκ·Έλž˜λ°μ—μ„œμ˜ 객체(Object)λž€ λ¬΄μ—‡μΌκΉŒμš”?

  • 객체 지ν–₯ ν”„λ‘œκ·Έλž˜λ°(Object-Oriented Programming, OOP)μ—μ„œμ˜ 객체(Object)λŠ” 클래슀(Class)에 μ˜ν•΄ μ •μ˜λœ 데이터와 κ·Έ 데이터λ₯Ό μ²˜λ¦¬ν•˜λŠ” λ™μž‘(λ©”μ†Œλ“œ, Method)을 ν¬ν•¨ν•˜λŠ” 독립적인 κ°œμ²΄μž…λ‹ˆλ‹€.
  • 객체(Object)λŠ” ν”„λ‘œκ·Έλž¨ λ‚΄μ—μ„œ μƒνƒœ(속성 λ˜λŠ” ν•„λ“œ)와 ν–‰μœ„(λ©”μ†Œλ“œ λ˜λŠ” ν•¨μˆ˜)λ₯Ό 가지며, μ΄λŸ¬ν•œ μƒνƒœμ™€ ν–‰μœ„λ₯Ό 톡해 ν˜„μ‹€ μ„Έκ³„μ˜ 사물을 λͺ¨λΈλ§ν•˜κ±°λ‚˜ μ‹œμŠ€ν…œ λ‚΄μ˜ κ°œλ…μ„ μΆ”μƒν™”ν•˜λŠ” λ°©μ‹μœΌλ‘œ μ‚¬μš©λ©λ‹ˆλ‹€.

πŸ™‹β€β™‚οΈ 좔상화(Abstraction)
πŸ™‹β€β™‚οΈ DIP의 μ •μ˜μ—μ„œ λ§ν•˜λŠ” β€˜μΆ”μƒν™”λœ κ²ƒβ€™μ΄λž€ λ¬΄μ—‡μΌκΉŒ?
πŸ™‹β€β™‚οΈ DIP의 μ •μ˜μ—μ„œ λ§ν•˜λŠ” β€˜μΆ”μƒν™”λœ 것’과 β€˜μΆ”μƒν™”β€™μ˜ κ°œλ…μ˜ 차이점.

1️⃣ 객체의 ꡬ성 μš”μ†Œ.

1️⃣ 속성(Attributes) λ˜λŠ” ν•„λ“œ(Fields)

  • 객체의 데이터λ₯Ό λ‚˜νƒ€λƒ…λ‹ˆλ‹€.
    • μ΄λŠ” 객체가 κ°€μ§€λŠ” μƒνƒœλ₯Ό ν‘œν˜„ν•˜λŠ” λ³€μˆ˜λ‘œ, ν΄λž˜μŠ€μ—μ„œ μ •μ˜λœ 속성(Attributes)에 따라 각 κ°μ²΄λŠ” κ³ μœ ν•œ 값을
  • 예λ₯Ό λ“€μ–΄, β€œμžλ™μ°¨β€ κ°μ²΄μ—λŠ” 색상, 속도 같은 속성이 μžˆμ„ 수 μžˆμŠ΅λ‹ˆλ‹€.
  • Javaμ—μ„œ 속성은 μΈμŠ€ν„΄μŠ€ λ³€μˆ˜λ‘œ ν‘œν˜„λ©λ‹ˆλ‹€.
class Car {
    private String color;
    private int speed;
    
    public Car(String color, int speed) {
        this.color = color; // 속성
        this.speed = this.speed; // 속성
    }
}

2️⃣ λ©”μ†Œλ“œ(Methods)

  • 객체의 행동을 μ •μ˜ν•˜λŠ” ν•¨μˆ˜μž…λ‹ˆλ‹€.
    • λ©”μ†Œλ“œλŠ” 객체의 데이터λ₯Ό μ‘°μž‘ν•˜κ±°λ‚˜ νŠΉμ • μž‘μ—…μ„ μˆ˜ν–‰ν•  수 μžˆμŠ΅λ‹ˆλ‹€.
  • 예λ₯Ό λ“€μ–΄, β€œμžλ™μ°¨β€ 객체(Object)λŠ” 달리기 λ˜λŠ” λ©ˆμΆ”κΈ° 같은 λ©”μ†Œλ“œ(Methods)λ₯Ό κ°€μ§ˆ 수 μžˆμŠ΅λ‹ˆλ‹€.
  • Javaμ—μ„œ λ©”μ†Œλ“œ(Methods)λŠ” ν•¨μˆ˜λ‘œ μ •μ˜λ©λ‹ˆλ‹€.
class Car {
    private String color;
    private int speed;
    
    public Car(String color, int speed) {
        this.color = color;
        this.speed = speed;
    }
    
    public void run() {
        System.out.println("The car is running at " + this.speed + " km/h"); // λ©”μ†Œλ“œ(Methods)
    }
}

3️⃣ 클래슀(Class)

  • 객체(Object)λ₯Ό μƒμ„±ν•˜κΈ° μœ„ν•œ 청사진 λ˜λŠ” μ„€κ³„λ„μž…λ‹ˆλ‹€.
    • ν΄λž˜μŠ€λŠ” 객체의 속성(Attributes)κ³Ό λ©”μ†Œλ“œ(Methods)λ₯Ό μ •μ˜ν•˜λ©°, 객체(Object)λŠ” 이 클래슀(Class)λ₯Ό 기반으둜 μƒμ„±λ©λ‹ˆλ‹€.
  • 예λ₯Ό λ“€μ–΄, β€œCarβ€λΌλŠ” 클래슀(Class)λŠ” μžλ™μ°¨μ˜ 속성(색상, 속도)κ³Ό 행동(달리기, λ©ˆμΆ”κΈ°)을 μ •μ˜ν•˜κ³ , 이 클래슀(Class)λ₯Ό μ‚¬μš©ν•΄ λ‹€μ–‘ν•œ β€œμžλ™μ°¨β€ 객체(Object)λ₯Ό λ§Œλ“€ 수 μžˆμŠ΅λ‹ˆλ‹€.
  • Javaμ—μ„œ ν΄λž˜μŠ€λŠ” λ‹€μŒκ³Ό 같이 μ •μ˜λ©λ‹ˆλ‹€.
class Car {
    private String color;
    private int speed;
    
    public Car(String color, int speed) {
        this.color = color;
        this.speed = speed;
    }
    
    public void run() {
        System.out.println("The car is running at " + this.speed + " km/h");
    }
}

4️⃣ μΈμŠ€ν„΄μŠ€(Instance)

  • 클래슀(Class)λ‘œλΆ€ν„° μƒμ„±λœ μ‹€μ œ 객체(Object)λ₯Ό μ˜λ―Έν•©λ‹ˆλ‹€.
    • ν•˜λ‚˜μ˜ 클래슀(Class)λŠ” μ—¬λŸ¬ 개의 μΈμŠ€ν„΄μŠ€(Instance)λ₯Ό κ°€μ§ˆ 수 있으며, 각 μΈμŠ€ν„΄μŠ€λŠ” κ³ μœ ν•œ 속성(Attributes) 값을 κ°€μ§ˆ 수 μžˆμŠ΅λ‹ˆλ‹€.
  • 예λ₯Ό λ“€μ–΄,”Car” ν΄λž˜μŠ€μ—μ„œ β€œred_carβ€λΌλŠ” μΈμŠ€ν„΄μŠ€(Instance)λ₯Ό 생성할 수 μžˆμŠ΅λ‹ˆλ‹€.
Car redCar = new Car("red", 120);
redCar.run(); // "The car is running at 120 km/h" 좜λ ₯

2️⃣ 객체의 νŠΉμ„±.

1️⃣ μΊ‘μŠν™”(Encapsulation)

  • 객체(Object)λŠ” μžμ‹ μ˜ 데이터λ₯Ό μ™ΈλΆ€λ‘œλΆ€ν„° μ€λ‹‰ν•˜κ³ , ν•΄λ‹Ή 데이터λ₯Ό μ‘°μž‘ν•˜λŠ” λ©”μ†Œλ“œ(Methods)λ₯Ό ν†΅ν•΄μ„œλ§Œ 접근을 ν—ˆμš©ν•˜λŠ” νŠΉμ„±μ„ κ°€μ§‘λ‹ˆλ‹€.
    • 이λ₯Ό 톡해 λ°μ΄ν„°μ˜ 무결성을 μœ μ§€ν•  수 μžˆμŠ΅λ‹ˆλ‹€.
  • μΊ‘μŠν™”(Encapsulation)λŠ” 객체(Object) λ‚΄λΆ€ κ΅¬ν˜„ μ„ΈλΆ€ 사항을 μ™ΈλΆ€μ—μ„œ μ•Œ ν•„μš” 없이 μΈν„°νŽ˜μ΄μŠ€(Interface)λ§Œμ„ 톡해 μƒν˜Έμž‘μš©ν•  수 μžˆλ„λ‘ ν•©λ‹ˆλ‹€.

2️⃣ 좔상화(Abstraction)

  • 객체(Object)λŠ” ν˜„μ‹€ μ„Έκ³„μ˜ μ‚¬λ¬Όμ΄λ‚˜ κ°œλ…μ„ μΆ”μƒν™”ν•˜μ—¬ λ‚˜νƒ€λƒ…λ‹ˆλ‹€.
    • λ³΅μž‘ν•œ μ‹œμŠ€ν…œμ„ λ‹¨μˆœν™”ν•˜μ—¬ μ€‘μš”ν•œ μ •λ³΄λ§Œμ„ ν‘œν˜„ν•˜κ³ , λΆˆν•„μš”ν•œ μ„ΈλΆ€ 사항을 μˆ¨κΉλ‹ˆλ‹€.
  • 예λ₯Ό λ“€μ–΄, μžλ™μ°¨μ˜ λ³΅μž‘ν•œ 엔진 λ‚΄λΆ€ κ΅¬μ‘°λŠ” 숨기고, μ‚¬μš©μžλŠ” λ‹¨μˆœνžˆ β€œλ‹¬λ¦¬κΈ°β€ λ©”μ†Œλ“œ(Methods)둜 μžλ™μ°¨λ₯Ό 움직일 수 μžˆμŠ΅λ‹ˆλ‹€.

3️⃣ 상속(Inheritance)

  • 객체 지ν–₯ ν”„λ‘œκ·Έλž˜λ°μ—μ„œ 상속(Inheritance)은 ν•œ ν΄λž˜μŠ€κ°€ λ‹€λ₯Έ 클래슀의 속성(Attributes)κ³Ό λ©”μ†Œλ“œ(Methods)λ₯Ό λ¬Όλ €λ°›λŠ” 것을 μ˜λ―Έν•©λ‹ˆλ‹€.
    • 이λ₯Ό 톡해 κΈ°μ‘΄ 클래슀의 κΈ°λŠ₯을 ν™•μž₯ν•˜κ±°λ‚˜ μˆ˜μ •ν•˜μ—¬ μƒˆλ‘œμš΄ 클래슀λ₯Ό μ •μ˜ν•  수 μžˆμŠ΅λ‹ˆλ‹€.
  • 예λ₯Ό λ“€μ–΄, β€œCar” ν΄λž˜μŠ€λŠ” β€œElectricCarβ€λΌλŠ” ν•˜μœ„ 클래슀둜 ν™•μž₯될 수 μžˆμŠ΅λ‹ˆλ‹€.
    class ElectricCar extends Car {
      private int batteryCapacity;
        
      public ElectricCar(String color, int speed, int batteryCapacity) {
          super(color, speed);
          this.batteryCapacity = batteryCapacity;
      }
    }
    

4️⃣ λ‹€ν˜•μ„±(Polymorphism)

  • λ‹€ν˜•μ„±(Polymorphism)은 객체(Object)κ°€ 같은 μΈν„°νŽ˜μ΄μŠ€(Interface)λ₯Ό μ‚¬μš©ν•˜μ—¬ λ‹€μ–‘ν•œ λ°©μ‹μœΌλ‘œ λ™μž‘ν•  수 μžˆλŠ” νŠΉμ„±μž…λ‹ˆλ‹€.
    • 즉, λ™μΌν•œ λ©”μ†Œλ“œ(Methods)κ°€ λ‹€μ–‘ν•œ ν΄λž˜μŠ€μ—μ„œ λ‹€λ₯΄κ²Œ κ΅¬ν˜„λ  수 μžˆμŠ΅λ‹ˆλ‹€.
  • 예λ₯Ό λ“€μ–΄, β€œAnimalβ€μ΄λΌλŠ” ν΄λž˜μŠ€μ— β€œspeak()β€λΌλŠ” λ©”μ†Œλ“œκ°€ μžˆλ‹€λ©΄, 이λ₯Ό μƒμ†λ°›λŠ” β€œDog”와 β€œCat” ν΄λž˜μŠ€λŠ” 각각의 λ°©μ‹μœΌλ‘œ 이 λ©”μ†Œλ“œλ₯Ό λ‹€λ₯΄κ²Œ κ΅¬ν˜„ν•  수 μžˆμŠ΅λ‹ˆλ‹€.
class Animal {
    public void speak() {
        // λ©”μ†Œλ“œ κ΅¬ν˜„ μƒλž΅
    }
}

class Dog extends Animal {
    @Override
    public void speak() {
        System.out.println("Woof");
    }
}

class Cat extends Animal {
    @Override
    public void speak() {
        System.out.println("Meow");
    }
}

3️⃣ 객체의 μ˜ˆμ‹œ.

1️⃣ μžλ™μ°¨ 객체 μ˜ˆμ‹œ.

class Car {
    private String color;
    private int speed;
    
    public Car(String color, int speed) {
        this.color = color;
        this.speed = speed;
    }
    
    public void run() {
        System.out.println("The " + this.color + " car is running at " + this.speed + " km/h");
    }
    
    public class Main {
        public static void main(String[] args) {
            // μΈμŠ€ν„΄μŠ€ 생성
            Car myCar = new Car("red", 100);
            myCar.run(); // "The red car is running at 100 km/h" 좜λ ₯
        }
    }
}

2️⃣ 은행 κ³„μ’Œ 객체 μ˜ˆμ‹œ.

class BankAccount {
    private String accountNumber;
    private double balance;
    
    public BankAccount(String accountNumber, double balance) {
        this.accountNumber = accountNumber;
        this.balance = balance;
    }
    
    public void deposit(double amount) {
        balance += amount;
        System.out.println(amount + " deposited. Ned balance: " + balance);
    }
    
    public void withdraw(double amount) {
        if (balance >= amount) {
            balance -= amount;
            System.out.println(amount + " withdrawn. Remaining balance: " + balance);
        } else {
            System.out.println("Insufficient balance");
        }
    }
}

public class Main {
    public static void main(String[] args) {
        // μΈμŠ€ν„΄μŠ€ 생성.
        BankAccount myAccount = new BankAccount("123-456", 5000);
        myAccount.deposit(1000); // "1000 deposited. New balance: 6000" 좜λ ₯
        myAccount.withdraw(2000); // "2000 withdraw Remaining balance: 4000" 좜λ ₯
    }
}

4️⃣ κ²°λ‘ .

  • 객체(Object)λŠ” 클래슀(Class)에 μ •μ˜λœ 속성(Attributes)κ³Ό λ©”μ†Œλ“œ(Methods)λ₯Ό 가진 독립적인 개체둜, 객체 지ν–₯ ν”„λ‘œκ·Έλž˜λ°μ˜ 핡심 ꡬ성 μš”μ†Œμž…λ‹ˆλ‹€.
  • 객체(Object)λŠ” ν˜„μ‹€ μ„Έκ³„μ˜ κ°œλ…μ΄λ‚˜ μ‹œμŠ€ν…œμ˜ ꡬ성 μš”μ†Œλ₯Ό ν”„λ‘œκ·Έλž˜λ°μ μœΌλ‘œ ν‘œν˜„ν•˜λ©°, μƒνƒœ(속성)와 ν–‰μœ„(λ©”μ†Œλ“œ)λ₯Ό 가지고 μžˆμŠ΅λ‹ˆλ‹€.
  • κ°μ²΄λŠ” 좔상화, μΊ‘μŠν™”, 상속, λ‹€ν˜•μ„±κ³Ό 같은 객체 지ν–₯ 원칙을 λ”°λ₯΄λ©°, μž¬μ‚¬μš© κ°€λŠ₯μ„±κ³Ό μœ μ§€λ³΄μˆ˜μ„±μ„ ν–₯상 μ‹œν‚΅λ‹ˆλ‹€.