21_class.html 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. </head>
  8. <body>
  9. <script>
  10. // 创建一个类
  11. class Person{
  12. constructor(userName,userAge){
  13. this.userName = userName;
  14. this.userAge = userAge;
  15. }
  16. talk(){
  17. console.log(`大家好我叫${this.userName}今年${this.userAge}岁了`);
  18. }
  19. }
  20. // 实例化一个对象
  21. // let p1 = new Person('张三',18);
  22. // console.log(p1);
  23. // p1.talk();
  24. // 实现类的继承
  25. class Student extends Person{
  26. constructor(userName,userAage,school){
  27. super(userName,userAage);
  28. this.school = school;
  29. }
  30. sayHello(){
  31. console.log("hello world");
  32. }
  33. }
  34. let s1 = new Student('李四',18,'清华大学');
  35. console.log(s1.userName);
  36. s1.talk();
  37. s1.sayHello();
  38. </script>
  39. </body>
  40. </html>