//상속
class Fish {
constructor(name, age) {
this.name = name;
this.age = age;
}
swim() {
console.log('어푸어푸 붕어빵');
}
}
class Shark extends Fish {
}
const fish1 = new Fish('붕어빵1', 1);
const shark1 = new Shark('상어빵1', 1);
console.log(`상어빵: ${shark1.name}, ${shark1.age}살`);
// 부모 클래스
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
greet() {
console.log("Hello, my name is " + this.name);
}
}
// 자식 클래스
class Student extends Person {
constructor(name, age, grade) {
// 부모 클래스의 생성자 호출
super(name, age);
this.grade = grade;
}
study() {
console.log(this.name + " is studying in grade " + this.grade);
}
}
// 객체 생성 및 메서드 호출
const student1 = new Student("Alice", 20, "A");
student1.greet(); // Hello, my name is Alice
student1.study(); // Alice is studying in grade A
'Language > Javascript' 카테고리의 다른 글
[JS] 객체와 배열 (0) | 2024.07.25 |
---|---|
[JS] <input type="radio"> 선택한 값 활용하기 (0) | 2024.07.25 |
[JS] 자바스크립트 기초 (코딩애플 유튜브 무료강의) (0) | 2024.03.28 |
[JS] jQuery (0) | 2023.10.23 |
[JS] DOM (0) | 2023.09.07 |