Skip to content

[1장] 2.6.1 클래스 지향 설계에 대하여 #1

@seohag

Description

@seohag

73 페이지

  1. 클래스 지향 설계는 상속과 다형성이 중요하고, 상속받은 메서드와 새롭게 정의한 메서드의 이름이 동일하고 공존할 수 있는 것이 다형성 이라고 기재되어 있고
  2. 객체 지향 프로그래밍에서 다형성은 메서드 오버라이딩 메서드 오버로딩 두 가지로 분류된다고 조사했습니다. 현재 정리되어 있는 코드는 상속 개념에 대한 예제가 작성되어 있는 것 같은데 메서드 오버라이딩과 오버로딩의 차이를 알고 싶습니다!
// 메서드 오버라이딩

class Animal {
  makeSound() {
    console.log("Animal is making a sound.");
  }
}

class Dog extends Animal {
  makeSound() {
    console.log("Dog is barking.");
  }
}

class Cat extends Animal {
  makeSound() {
    console.log("Cat is meowing.");
  }
}

const animal = new Animal();
const dog = new Dog();
const cat = new Cat();

animal.makeSound(); // Animal is making a sound.
dog.makeSound(); // Dog is barking.
cat.makeSound(); // Cat is meowing.
// 메서드 오버로딩

class Calculator {
  add(a: number, b: number): number {
    return a + b;
  }
  
  add(a: string, b: string): string {
    return a + b;
  }
}

const calculator = new Calculator();

console.log(calculator.add(1, 2)); // 3
console.log(calculator.add("Hello, ", "world!")); // Hello, world!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions