ES6中面向对象可以继承:
1、ES6中的继承使用关键字 extends
2、调用父类构造使用super()
案例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>面向对象之继承</title>
<script>
// ES6中的继承使用关键字 extends
// 调用父类构造使用super()
// 定义一个父类
class Person {
// 构造器
constructor(name, age){
this.name = name
this.age = age
}
// getName
getName(){
return this.name
}
// setName
setName(name){
this.name = name
}
// getAge
getAge(){
return this.age
}
// setAge
setAge(age){
this.age = age;
}
}
// 继承Person类
class People extends Person {
constructor(name , age, sex) {
super(name, age)
this.sex = sex
}
//getSex
getSex(){
return this.sex
}
//setSex
setSex(sex){
this.sex = sex
}
}
// 创建People实例
let people = new People('李四', 30, '男')
alert(people.getName());
alert(people.getAge());
alert(people.getSex());
people.setName('李思');
people.setAge(31);
people.setSex('女')
alert(people.getName());
alert(people.getAge());
alert(people.getSex());
</script>
</head>
<body>
</body>
</html>
网友评论