12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- <!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 声明
- class Person {
- // 1.属性 构造器下
- constructor(name,age){
- this.name = name;
- this.age = age;
- }
- // 2.方法
- say(){
- console.log("我叫" +this.name,"今年"+this.age+"岁");
- }
- // static 类调用
- static hello() {
- console.log("您好")
- }
- }
- let p1 = new Person("John",10);
- p1.name = '小小'
- console.log(p1);
- p1.say();
- // 实例化调用
- Person.hello();
- // p1.hello();
- // 类的继承 extends
- class Child extends Person {
- constructor(name,age,hobby) {
- // super 继承父类
- super(name,age);
- this.hobby = hobby
- }
- hi1() {
- console.log("hi1")
- }
- }
- let c1 = new Child("Lucy",20,'羽毛球');
- console.log(c1);
- c1.say();
- c1.hi1()
- Child.hello();
- </script>
- </body>
- </html>
|