123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Document</title>
- </head>
- <body>
- <script>
- // 类的定义
- class Person{
- // 类的构造函数
- constructor(name,age){
- this.username = name;
- this.age = age;
- }
- // 类的方法
- talk(){
- console.log(`我叫${this.username},我今年${this.age}岁`)
- }
- }
- // let p1 = new Person("张三",18);
- // console.log(p1.username);
- // p1.talk();
- // 类的继承
- class Teacher extends Person{
- constructor(name,age,school){
- super(name,age);
- this.school = school;
- }
- // 私有方法
- showSchool(){
- console.log("我的学校是"+this.school);
- }
- }
- let t1 = new Teacher("张三",18,"清华大学");
- console.log(t1.username);
- t1.talk();
- t1.showSchool();
- </script>
- </body>
- </html>
|