复习挑战
本课属于 Coddy JavaScript 学习旅程的“面向对象编程”单元。第 29 课,共 56 课。
挑战
使用继承创建家具类
任务 1:创建 Chair 类
创建一个继承 Furniture 的 Chair 类:
- 在构造函数中使用
super(),从父类继承material和color - 添加属性:
numberOfLegs - 添加方法:
sit(),返回"Sitting on the chair"
任务 2:创建 Table 类
创建一个继承 Furniture 的 Table 类:
- 在构造函数中使用
super(),从父类继承material和color - 添加属性:
shape - 添加方法:
placeItem(),返回"Item placed on table"
亲手试试
import { Furniture } from './furniture.js';
import { Table } from './table.js';
import { Chair } from './chair.js';
// 测试代码,请勿修改
const myChair = new Chair("wood", "brown", 4);
console.log(myChair.describe()); // 应输出 "brown wood furniture"
console.log(myChair.numberOfLegs); // 应输出 4
console.log(myChair.sit()); // 应输出 "Sitting on the chair"
const myTable = new Table("glass", "clear", "round");
console.log(myTable.describe()); // 应输出 "clear glass furniture"
console.log(myTable.shape); // 应输出 "round"
console.log(myTable.placeItem()); // 应输出 "Item placed on table"面向对象编程中的所有课
8整理 OOP 代码
将类组织到模块中自己练习: JavaScript 在线编译器