본문 바로가기

TIL

[JavaScript] 5주차_클래스 상속

// 상속 (Inheritance)
// Class -> 유산으로 내려주는 주요 기능
// 부모 <--> 자식

// 동물 전체에 대한 클래스
class Animal {
  constructor(name) {
    this.name = name;
  }

  //메서드 (짖다)
  speak() {
    console.log(`${this.name} says!`);
  }
}

const me = new Animal("Mia");
me.speak(); // Mia says!

class Dog extends Animal {
  // 상속 : 부모에게서 내려받은 메서드를 재정의 할 수 있음.
  // 오버라이딩(overriding)
  speak() {
    console.log(`${this.name} barks!`);
  }
}

const cuttyPuppy01 = new Dog("Buttercup");
cuttyPuppy01.speak(); // Buttercup barks!

'TIL' 카테고리의 다른 글

TIL 23.06.22 스타일 컴포넌트, 인풋값 오류  (0) 2023.06.23
WIL 23.06.18 [개인프로젝트] TO DO LIST  (0) 2023.06.18
TIL 23.05.31  (0) 2023.05.31
[JavaScript] 5주차_클래스 생성 연습  (0) 2023.05.31
TIL 23.05.28  (0) 2023.05.28