天天看點

class 類的繼承

  1. 子類可以繼承父類的全部内容(包括靜态方法)
{

        class Person {
            constructor(n, s) {
                this.name = n;
                this.sex = s;
            }

            eat() {
                console.log("吃飯");
            }
            static work() {
                console.log("靜态方法");
            }
        }
        class Children extends Person {
            constructor() {
                super();//調用父類的構造函數
            }
        }
        let s = new Children();
        console.log(s);
        Children.work();//靜态方法
    }
           
  1. 多個繼承
{

        class Person {
            constructor(n, s) {
                this.name = n;
                this.sex = s;
            }

            eat() {
                console.log("吃飯");
            }

            static work() {
                console.log("....");
            }
        }
        class Children extends Person {
            constructor(m, n, s) {
                super(n, s)
                this.money = m;
            }
        }
        class Student extends Children {
            constructor(n, s) {
                super("錢", n, s);//調用父類的構造函數
            }

            job() {
                console.log("上學");
            }
        }
        let s = new Student();
        console.log(s);
    }