JSFiddle - React, Tailwind, and code Playground
by yshrkn
JavaScript
// 親クラス
class Human {
constructor(name, age) {
this.name = name;
this.age = age;
}
sayHello() {
console.log(`こんにちは。私は${this.name}。今年${this.age}歳です。`);
}
}
// サブクラス
class Superman extends Human {
constructor(name, age) {
super(name, age); // 基底クラスのコンストラクタを呼び出し、パラメータを渡す
}
sayHello() {
super.sayHello();
console.log(`地球を守ります。`);
}
}
const superman = new Superman("スーパーマン", 100);
superman.sayHello(); // こんにちは。私はundefined。今年undefined歳です。
// 地球を守ります。