1-1 静态方法
静态方法的好处就是不用生成类的实例就可以直接调用。
static方法修饰的成员不再属于某个对象,而是属于它所在的类。只需要通过其类名就可以访问,不需要再消耗资源反复创建对象。
在类第一次加载的时候,static就已经在内存中了,直到程序结束后,该内存才会释放。
# 静态方法的特点1.只能通过类名调用2.不能再静态方法中使用普通方法3.静态方法中this指调用静态方法的类4.静态方法是可以被继承的
class Student{static say(){console.log("hello world")}}Student.say();let s = new Student();s.say() //报错
1-2 静态方法中的this
静态方法中的this指向类。
class Student{static init(){this.say();Student.say();}static say(){console.log("hello world")}}Student.init();
1-3 静态属性
class Student{static class = "html5"static init(){console.log("hello world")}}console.log(Student.class)
1-4 继承
class Person{static sayName(){console.log("name")}}class Teacher extends Person{}Teacher.sayName();
