es6-类

 //在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();
        //如果子类中的构造方法和父类一致会覆盖,子类可以继承一个
        //父类,但是可以链表式子的继承。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值