π [Section2_Unit2] κ°μ²΄ μ§ν₯ νλ‘κ·Έλλ° #5
βοΈ λΆλͺ¨ ν΄λμ€μμ μμλ°μμ€λ μμ ν΄λμ€μμ μ¬μ©λλ€.
class Parent{ ... }
class Child extends Parent { ... }
βοΈ μ¬λ¬ μμ±μ μμλ°μ λλ super()
μ μΈμ μ¬λ¬κ°λ₯Ό μ€λ€. (superλ νλ²λ§ νΈμΆ κ°λ₯)
βοΈ λ¬΄μ‘°κ±΄ super()
μ΄νμ this
λ₯Ό μΈ μ μμ.
class Person{
constructor(name,age, food){
this.name = name;
this.age = age;
this.food = food;
}
}
class Student extends Person{
constructor(name, age, food, grades){
super(name, age, food);
this.grades = 'A'; // κΌ λ³μλ‘ μ£Όμ§ μκ³ κ°μ ν λΉν μλ μλ€.
this.food = "pizza"; // μλ‘κ² μ¬ν λΉλ ok
}
}
βοΈ μμ ν΄λμ€μμ μ€λ²λΌμ΄λ©ν λΆλͺ¨ ν΄λμ€μ λ©μλλ₯Ό νΈμΆνμ§ μμλ λλ κ²½μ°.
βοΈ λΆλͺ¨ ν΄λμ€μ μμ±μ ν¨μκ° μΈμλ₯Ό λ°μ§ μκ³ , λΆλͺ¨ ν΄λμ€μ μμ±μ ν¨μκ° νΈμΆλμ§ μμλ λλ κ²½μ°
class Animal {
constructor() {
this.type = 'animal';
}
}
class Cat extends Animal {
constructor() {
// λΆλͺ¨ ν΄λμ€μ μμ±μ ν¨μλ₯Ό νΈμΆνμ§ μμλ λ¨
this.name = 'cat';
}
}