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>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章