ES6 类class

                   Document     

//定义一个Person类
    class Person {
        //构造方法
        constructor(name,age) {
            //构造器中的this是谁? -------当前类的实例对象
            this.name = name;
            this.age = age;
        }
        //一般方法
        /*
            1.say放在那里? ----Person的原型对象上,供实例使用
            2.say中的this是谁? ----如果say是通过Person实例调用的,那么this就是Person的实例对象
        */ 
        say() {
            console.log(我叫${this.name},今年${this.age})
        }
    }
    //定义一个Person的实例对象
    var p =new Person(‘guo’,18);
    console.log§
    console.log(p.name);
    p.say();

//定义一个Student类继承于Person
    class Student extends Person{
        constructor(name,age,greate) {
            super(name,age)
            this.greate = greate
        }
        //重写从父类继承过来的方法
        say() {
            console.log(我叫${this.name},今年${this.age},读${this.greate} )
        }
        /*
            1.study放在那里? ----Student的原型对象上,供实例使用
            2.study中的this是谁? ----如果study是通过Student实例调用的,那么this就是Student的实例对象
        */ 
        study() {
            console.log(“我在努力学习”,this)
        }
    }

let s = new Student(‘hang’,19,‘二年级’);
    console.log(s)
    s.say()
    s.study()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值