override
사전적의미 : 기각하다, 중단시키다.
= 덮어쓰기
class Model {
name;
year;
constructor(name, year) {
this.name = name;
this.year = year;
}
sayHello() {
return `안녕하세요 ${this.name} 입니다.`
}
}
class FemaleModel extends Model {
part;
constructor(name, year, part) {
super(name, year); // 부모클래스
this.part = part;
}
sayHello() {
return `${super.sayHello()} ${this.part}를 맡고있습니다. `
}
}
// 자식 클래스에서 부모클래스의 생성자를 덮어쓰기(override)할때 자식클래스에서 생성자를 생성해주며, 기존생성자를 그대로 사용하고 싶을 경우 super keyword를 사용한다.(생성자또한 상속받게되어 super 키워드로 사용가능)
const wonYoung = new FemaleModel('이원영', 1997, "코딩");
console.log(wonYoung);
const wonYoung2 = new Model('김원영', 1999);
console.log(wonYoung2.sayHello());
console.log(wonYoung.sayHello());