es6类的继承

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>类的继承</title>
  </head>
  <body>
  <script src="js/jquery.js" charset="utf-8"></script>
  <script type="text/javascript">
  //定义一个类
     class A {
      constructor(name,age,gender) {//自己的属性
        this.name=name;
        this.age=age;
        this.gender=gender;
      }
      showId(){
        console.log('我的名字叫'+this.name+',今年'+this.age+'岁,我的性别是'+this.gender);
      }
      static loadAll(){
        console.log("我是一个私有的方法");
      }
    }

    let aa=new A('李特成',28,"男");//实例化
    aa.showId();//调用自己的方法
    //aa.loadAll();//aa.loadAll is not a function 静态属性 不能被调用
    A.loadAll();//静态属性只有自己才能调用
    console.log(typeof A); // 它是一个function
    console.log(aa instanceof A); // true //判断aa 是不是A的实例

   //继承A
   class B extends A {
      constructor(name,age,gender,address){
        super(name,age,gender);//调用父类的constructor
        this.address=address;//自己的属性
      }
      toFun(){
        console.log('我的名字叫'+this.name+',今年'+this.age+'岁,我的性别是'+this.gender+',家住'+this.address);
      }

   }
   let bb=new B('李特成',28,"男",'湖南');//实例化
       console.log(bb instanceof B); // true //判断bb 是不是 BB的实例
        bb.showId();//调用父类的方法
        bb.toFun();//调用自己的方法
        B.loadAll();//loadAll()是A类的静态方法,B继承A,也继承了A的静态方法。
  </script>

  </body>
</html>

猜你喜欢

转载自blog.csdn.net/a4561614/article/details/80816638