11.es6类.html 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. // 类 class 声明
  11. class Person {
  12. // 1.属性 构造器下
  13. constructor(name,age){
  14. this.name = name;
  15. this.age = age;
  16. }
  17. // 2.方法
  18. say(){
  19. console.log("我叫" +this.name,"今年"+this.age+"岁");
  20. }
  21. // static 类调用
  22. static hello() {
  23. console.log("您好")
  24. }
  25. }
  26. let p1 = new Person("John",10);
  27. p1.name = '小小'
  28. console.log(p1);
  29. p1.say();
  30. // 实例化调用
  31. Person.hello();
  32. // p1.hello();
  33. // 类的继承 extends
  34. class Child extends Person {
  35. constructor(name,age,hobby) {
  36. // super 继承父类
  37. super(name,age);
  38. this.hobby = hobby
  39. }
  40. hi1() {
  41. console.log("hi1")
  42. }
  43. }
  44. let c1 = new Child("Lucy",20,'羽毛球');
  45. console.log(c1);
  46. c1.say();
  47. c1.hi1()
  48. Child.hello();
  49. </script>
  50. </body>
  51. </html>