클래스의 상속 (sub classing)
class Goodcoder {
constructor(age, lang, address) {
this.age = age;
this.lang = lang;
this.address = address;
}
coding() {
console.log(`내가 공부중인 언어는 ${this.lang}입니다.`)
}
}
이 아래에 새로운 클래스를 만들고 싶은데, class Goodcoder의 값을 상속시키고 싶다면 extends를 넣어 간단하게 처리할 수 있다.
class Employee extends Goodcoder {}
이렇게 새 클래스를 만들어 extends로 상속을 하면, 새로운 클래스에 이전 클래스 값들이 상속된다.
const noran = new Employee(20,'Java','Seoul');
noran.coding(); //내가 공부중인 언어는 Java입니다.
* 상속 자식은 여러명을 가질 수 있다.
상속된 자식에서의 수정도 가능하다. (이전 부모의 속성도 같이 불러오고 싶다면 super를 붙이면 된다.)
class Goodcoder {
constructor(age, lang, address) {
this.age = age;
this.lang = lang;
this.address = address;
}
coding() {
console.log(`내가 공부중인 언어는 ${this.lang}입니다.`)
}
}
class Employee extends Goodcoder {
coding() {
super.coding();
console.log(`나는 ${this.lang}를 마스터했습니다.`)
}
}
const noran = new Employee(20,'Java','Seoul');
noran.coding();
//내가 공부중인 언어는 Java입니다.
//나는 Java를 마스터했습니다.
출처 : 드림코딩 https://www.youtube.com/@dream-coding / MDN https://developer.mozilla.org/ko
'🟨 JavaScript > 개념' 카테고리의 다른 글
| [mdn, 드림코딩 외] 객체(Object)와 프로퍼티(property) (2) : 생성/수정과 cloning (0) | 2022.05.31 |
|---|---|
| [mdn, 드림코딩 외] 객체지향 (5) : instanceOf 연산자 (0) | 2022.05.25 |
| [mdn, 드림코딩 외] 객체지향 (3) : 정적 속성과 메소드(static properties and methods) (0) | 2022.05.04 |
| [mdn, 드림코딩 외] 객체지향 (2) : 객체(object)와 getter and setter (0) | 2022.05.04 |
| [mdn, 드림코딩 외] 객체지향 (1) : 클래스(class) (0) | 2022.05.03 |