• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            專職C++

            不能停止的腳步

              C++博客 :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
              163 Posts :: 7 Stories :: 135 Comments :: 0 Trackbacks

            常用鏈接

            留言簿(28)

            我參與的團隊

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            一、說明

            在es6中增加了一個class,簡單明了,比ES5下面強多了 
            每個類都有會有一個構(gòu)造函數(shù)constructor。如果沒有申明,則會默認分配一個空的。 
            如果需要調(diào)父類的構(gòu)造函數(shù),需要在constructor第一行調(diào)用super,至于為什么,已經(jīng)有N多文檔說明了。 
            例如:

            class a {
            constructor(m) {
            this.m_m = m;
            }
            };
            class b extends a {
            constructor(m, m1) {
            super(m);
            this.m_m1 = m1;
            }
            };
            • 1
            • 2
            • 3
            • 4
            • 5
            • 6
            • 7
            • 8
            • 9
            • 10
            • 11
            • 1
            • 2
            • 3
            • 4
            • 5
            • 6
            • 7
            • 8
            • 9
            • 10
            • 11

            二、屬性方法

            在es6中,使用get和set來標明屬的讀寫函數(shù)

            class a {
            constructor(m) {
            this.m_m = m;
            }
            get m() { return this.m_m; }
            set m(v) { this.m_m = v; }
            };
            let testa = new a(1999);
            console.log(testa.m);
            • 1
            • 2
            • 3
            • 4
            • 5
            • 6
            • 7
            • 8
            • 9
            • 1
            • 2
            • 3
            • 4
            • 5
            • 6
            • 7
            • 8
            • 9

            在這里就會 打印出1999

            三、非靜態(tài)成員函數(shù)

            class a {
            constructor(m) {
            this.m_m = m;
            }
            get m() { return this.m_m; }
            set m(v) { this.m_m = v; }
            mult(k) {
            return this.m_m * k;
            }
            add(k) {
            return this.m_m + k;
            }
            sub(k) {
            return this.m_m / k;
            }
            };
            • 1
            • 2
            • 3
            • 4
            • 5
            • 6
            • 7
            • 8
            • 9
            • 10
            • 11
            • 12
            • 13
            • 14
            • 15
            • 16
            • 1
            • 2
            • 3
            • 4
            • 5
            • 6
            • 7
            • 8
            • 9
            • 10
            • 11
            • 12
            • 13
            • 14
            • 15
            • 16

            在這里可以看到,不需要用function了,又是做了大大的簡化。

            四、靜態(tài)成員函數(shù)

            只需要在類的成員方法前,加一個static關(guān)鍵字就可以了,如果同一個類的靜態(tài)函數(shù),可以用this來調(diào)用。如下面的astatcfun1調(diào)用astatcfun,也可以用類名.的方問,如astatcfun2調(diào)用astatcfun,建議還是用this,在使用的時候,靜態(tài)方法,不需new出對象來,直接用對象.的方式,如下面的testcall調(diào)用a的靜態(tài)方法。也可以派生給子類。

            class a {
            constructor(m) {
            this.m_m = m;
            }
            static astaticfun() {
            return 100;
            }
            static astatcfun1() {
            return this.astaticfun() + 200;
            }
            static astatcfun2() {
            return a.astaticfun() + 200;
            }
            get m() { return this.m_m; }
            set m(v) { this.m_m = v; }
            mult(k) {
            return this.m_m * k;
            }
            add(k) {
            return this.m_m + k;
            }
            sub(k) {
            return this.m_m / k;
            }
            }; //
            function testcall(){
            console.log(a.astaticfun());
            console.log(a.astatcfun1());
            console.log(a.astatcfun2());
            }
            • 1
            • 2
            • 3
            • 4
            • 5
            • 6
            • 7
            • 8
            • 9
            • 10
            • 11
            • 12
            • 13
            • 14
            • 15
            • 16
            • 17
            • 18
            • 19
            • 20
            • 21
            • 22
            • 23
            • 24
            • 25
            • 26
            • 27
            • 28
            • 29
            • 30
            • 31
            • 32
            • 33
            • 34
            • 35
            • 36
            • 1
            • 2
            • 3
            • 4
            • 5
            • 6
            • 7
            • 8
            • 9
            • 10
            • 11
            • 12
            • 13
            • 14
            • 15
            • 16
            • 17
            • 18
            • 19
            • 20
            • 21
            • 22
            • 23
            • 24
            • 25
            • 26
            • 27
            • 28
            • 29
            • 30
            • 31
            • 32
            • 33
            • 34
            • 35
            • 36

            靜態(tài)成員函數(shù)的調(diào)用

            class a {
            static aaa() {
            return "aaa";
            }
            static bbb() {
            return this.aaa() + "bbb"; //同一個類的靜態(tài)函數(shù)調(diào)用函數(shù),只需要用this.就可以
            }
            kkk() { return "kkk"; }
            ccc() {
            return a.aaa() + "ccc" + this.kkk(); //同一個類的非靜態(tài)函數(shù)調(diào)用靜態(tài)函數(shù),則需要類名.的方式
            }
            };
            let c = new a();
            console.log(a.bbb());
            console.log(c.ccc());
            • 1
            • 2
            • 3
            • 4
            • 5
            • 6
            • 7
            • 8
            • 9
            • 10
            • 11
            • 12
            • 13
            • 14
            • 15
            • 16
            • 17
            • 18
            • 1
            • 2
            • 3
            • 4
            • 5
            • 6
            • 7
            • 8
            • 9
            • 10
            • 11
            • 12
            • 13
            • 14
            • 15
            • 16
            • 17
            • 18

            五、派生

            在ES6的派生通過extends這個關(guān)鍵字就可以,如

            class b extends a() { };
            posted on 2017-07-17 11:47 冬瓜 閱讀(642) 評論(0)  編輯 收藏 引用 所屬分類: javascript
            思思久久好好热精品国产| 色综合久久久久综合体桃花网| 成人久久综合网| 国内精品伊人久久久久| 久久久久国产一级毛片高清版| 国产精品亚洲美女久久久| 久久久久青草线蕉综合超碰| 91精品国产91热久久久久福利| 精品久久久久久国产| 国产综合精品久久亚洲| 国内精品久久久人妻中文字幕| 久久亚洲AV永久无码精品| 国内精品九九久久久精品| 亚洲欧美成人综合久久久| 99精品久久久久久久婷婷| 久久久无码精品亚洲日韩按摩| 无码人妻少妇久久中文字幕 | 国产巨作麻豆欧美亚洲综合久久| 三级片免费观看久久| 国产69精品久久久久99| 国产午夜免费高清久久影院| 久久AV无码精品人妻糸列| 亚洲国产成人久久一区WWW| 国产A级毛片久久久精品毛片| 99久久精品国内| 国产亚洲色婷婷久久99精品| 777午夜精品久久av蜜臀| 久久久国产视频| 久久久久久国产精品美女| 久久久精品国产| 久久久亚洲裙底偷窥综合| 久久精品国产亚洲AV蜜臀色欲 | 国产69精品久久久久9999| 91亚洲国产成人久久精品网址| 久久香蕉综合色一综合色88| .精品久久久麻豆国产精品| 日本久久久精品中文字幕| 国内精品久久久久久久coent| 久久久中文字幕日本| 色综合久久夜色精品国产 | 国产精品久久久久乳精品爆|