Skills/JavaScript

221227 class_07.html override

개발자 윤구나 2022. 12. 27. 11:49
<!DOCTYPE html>
<html lang="ko">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Class</title>
    <script>
      // override: 부모클래스가 가지고 있는 함수를 자식클래스에서 다시 선언하여 덮어쓰는 것

      class Information {
        constructor(name) {
          this.name = name;
        }

        myHobby() {
          document.write(`나 ${this.name}의 취미는 영화감상입니다.<br />`);
        }

        myHealth() {
          document.write(`나 ${this.name}은 매우 건강합니다.<br />`);
        }
      }

      const info = new Information("홍길동");
      info.myHobby();
      info.myHealth();

      class Child extends Information {
        constructor(name, weight) {
          super(name);
          this.weight = weight;
        }

        myHealth() {
          // override
          document.write(`나 ${this.name}의 몸무게는 ${this.weight}입니다.`);
        }
      }

      const child = new Child("윤동주", 45);
      child.myHobby();
      Child.myHealth();
    </script>
  </head>
  <body></body>
</html>