//在constructor里面不能直接定义属性,必须加this
class C1 {
say() {
console.log('hello,my name is zeng an ping')
}
constructor(a,b){
this.a = a;
this.b = b;
console.log('hello,我是构造方法')
}
static num = 1;
static say1() {
console.log('我是类的静态方法')
}
}
var c1 = new C1('zhang',12);
console.log(c1.a)
//类的方法是直接放在类中的函数,没有放在constructor中声明
//类的方法存在类的原型对象上,类本身不能访问,类的实例能访问。
c1.say();
C1.say1();
console.log(C1.num)
//类中最外层的非static属性,会作为实例化对象的属性,
//也不能成为类原型的属性。
//类的继承,解决代码的复用
class C2 extends C1 {
say() {
console.log('hello,我是子类中的构造方法')
}
constructor() {
super();
console.log('我是子类中的构造方法')
}
}
new C2();
var c2 = new C2();
console.log(C2.num);
C2.say1();
c2.say();
//如果子类中的构造方法和父类一致会覆盖,子类可以继承一个
//父类,但是可以链表式子的继承。
es6-类
最新推荐文章于 2024-09-11 15:33:18 发布