Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

Programmer

Class 본문

js

Class

Yuwel 2020. 6. 7. 15:02
//Animal 클래스
class Animal {
  //생성자
  constructor(type, name, sound) {
    this.type = type;
    this.name = name;
    this.sound = sound;
  }

  say() {
    console.log(this.sound);
  }
}

//Animal 클래스를 상속받는 Dog 클래스
class Dog extends Animal {
  //생성자
  constructor(name, sound) {
    super("개", name, sound);
  }
}
//Animal 클래스를 상속받는 Cat 클래스
class Cat extends Animal {
  //생성자
  constructor(name, sound) {
    super("고양이", name, sound);
  }
}

const dog = new Dog("멍멍이", "멍멍");
const cat = new Cat("야옹이", "야옹");

dog.say();
cat.say();
Comments