16-1 静态方法
属于类所独有的
特点:通过类名去调用
class Person{constructor(name,age){this.name=name;this.age=age;}static sayName(){console.log("name");}}var p=new Person("lisi",19)Person.sayName();
16-2静态方法和普通方法的调用
在普通方法中可以调用静态方法
在静态方法中不可以调用普通方法
class Person{constructor(name,age){this.name=name;this.age=age;}//在普通方法中可以调用静态方法sayAge(){Person.sayName();//调用静态方法console.log(this.age);}//在静态方法中不可以调用普通方法static sayName(){console.log("name");}}var p=new Person("lisi",19)p.sayAge();
16-3静态方法中的this
静态方法中的this,指向调用静态方法的类
class Person{constructor(name,age){this.name=name;this.age=age;}static sayName(){console.log(this);console.log("name");}}Person.sayName();
16-4静态方法的继承
静态方法可以被继承
子类可以继承父类的静态方法
在子类的静态方法中调用父类的静态方法,可以通过this/super调用
class Person{static baseUrl="https://www.baidu.com"static sayName(){console.log("name");}}class Teacher extends Person{static sayAge(){super.sayName();// this.sayName();console.log(super.baseUrl);console.log("age");}}Teacher.sayName();//nameconsole.log(Person.baseUrl);//https://www.baidu.com
