<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>类的继承</title>
</head>
<body>
<script>
class Animal{
constructor(name,age){
this.name=name;
this.age=age;
}
sayName(){
return this.name;
}
sayAge(){
return this.age;
}
}
class Dog extends Animal{
constructor(name,age,color){
super(name,age);
this.color=color;
}
sayColor(){
return `${this.name},今年:${this.age},颜色:${this.color}`;
}
sayAge(){
return "我今年"+super.sayAge()+"汪汪";
}
}
let dog=new Dog('旺财','2岁','黄色');
document.write(dog.sayColor());
document.write("<br/>");
document.write(dog.sayAge());
</script>
</body>
</html>
网友评论