<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
</body>
<script>
// 2.动物继承 (要求必须使用es6的语法糖(extends)实现继承):
// 1. 定义动物类(Animal) (3分)
// a) 属性
// i. 名称 name
// ii. 颜色 color
// b) 方法
class Animal {
constructor(name, color) {
this.name = name
this.color = color
}
}
// i. 吃 eat()
// 1. 打印格式: x色的xx在吃东西
Animal.prototype.eat = function () {
console.log(`${this.color}的${this.name}在吃东西`);
}
// 2. 定义猫类(Cat),继承Animal类 (4分)
class Cat extends Animal {
// a) 方法: 抓老鼠(catchMouse())
catchMouse() {
// i. 在方法中的输出格式为: 棕色的Tom猫在抓老鼠
console.log('棕色的Tom猫在抓老鼠 ');
}
}
// 3. 定义狗类(Dog),继承Animal类 (4分)
// var dog = new Dog()
class Dog extends Animal {
// a) 方法: 看家 (lookHome())
lookHome() {
// i. 在方法中的输出格式为: 白色的Jerry狗在看家
console.log('白色的Jerry狗在看家 ');
}
}
// 4. 实例化对象
// i. 创建Cat对象c,name赋值为Tom,color赋值为 棕色
var c = new Cat('Tom', '棕色');
// 调用c对象方法eat方法
c.eat()
// 调用c对象的方法catchMouse方法 (2分)
c.catchMouse()
// ii. 创建Dog对象 d,name赋值为Jerry,color赋值为 白色
var d = new Dog('Jerry', '白色');
// 调用d对象方法eat方法
d.eat()
// 调用d对象方法 lookHome方法 (2分)
d.lookHome()
</script>
</html>
动物继承 (使用es6的语法糖(extends)实现继承)
最新推荐文章于 2023-10-21 12:30:00 发布