0
点赞
收藏
分享

微信扫一扫

子类继承父类的演练 es6 230222

大雁f 2023-02-22 阅读 95

需求

定义一个狗类

让它继承动物类

在动物类中有属性

有方法


实例化狗对象

试一试让狗对象操作一下继承而来的属性与方法



代码


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script>

class Animal {
constructor(name, hp) {
this.name = name
this.hp = hp
}

sleep() {
console.log("动物在睡觉")
}
}

class Dog extends Animal {
constructor(name, hp, color) {
super(name, hp)
this.color = color
}
}

// 实例化一只狗
var wc = new Dog("旺财", 80, "白色")
console.log(wc)
// 调方法
wc.sleep()

</script>
</body>
</html>




举报

相关推荐

0 条评论