es6中 类的使用和类的继承

  1. 类的使用
class Child{
  constructor(){ // 构造器  里面的内容是私有属性
    this.age = '24';
  };
  // ES6的类只支持静态属性,不支持静态方法
  static a(){  // 属于类上的方法
    return '我是静态属性'
  };
  smoking(){ //原型上的方法
    console.log('smoking')
  };
};

let child  = new Child();
console.log(child.age)
console.log(child.smoking)
console.log(Child.a())  // 调new的实例才能获取静态属性

// 1. 类只能通过new 生成和调用
  1. 类的继承
// 类的继承
class Parent{
  constructor(){
    this.name = '我是父类';
    return {a: 1};
  };
  static b(){
    return '父类的静态属性'
  };
  eat(){
    console.log('父类的原型方法')
  };
};

class Childs extends Parent{ // es6新方法(extends)用于子类继承一个父类的公有和私有方法
    constructor(){
      super(); // 等价与Parent.call(this); 继承父类的私有属性
      this.child_name = '我是子类';
    };
    static a (){
      return '子类的静态属性'
    };
    smoking(){
      console.log('子类原型的方法')
    };
};

let childs = new Childs();
console.log(childs)
// console.log(childs.name)
// childs.eat()
// console.log(Childs.b())

// 1.类可以继承公有私有和静态方法
// 2.父类的构造函数中返回了一个引用类型会把这个引用类型作为子类的this
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值