• <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++博客 :: 首頁 :: 聯系 :: 聚合  :: 管理
              163 Posts :: 7 Stories :: 135 Comments :: 0 Trackbacks

            常用鏈接

            留言簿(28)

            我參與的團隊

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            一、說明

            在es6中增加了一個class,簡單明了,比ES5下面強多了 
            每個類都有會有一個構造函數constructor。如果沒有申明,則會默認分配一個空的。 
            如果需要調父類的構造函數,需要在constructor第一行調用super,至于為什么,已經有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來標明屬的讀寫函數

            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

            三、非靜態成員函數

            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了,又是做了大大的簡化。

            四、靜態成員函數

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

            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

            靜態成員函數的調用

            class a {
            static aaa() {
            return "aaa";
            }
            static bbb() {
            return this.aaa() + "bbb"; //同一個類的靜態函數調用函數,只需要用this.就可以
            }
            kkk() { return "kkk"; }
            ccc() {
            return a.aaa() + "ccc" + this.kkk(); //同一個類的非靜態函數調用靜態函數,則需要類名.的方式
            }
            };
            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這個關鍵字就可以,如

            class b extends a() { };
            posted on 2017-07-17 11:47 冬瓜 閱讀(641) 評論(0)  編輯 收藏 引用 所屬分類: javascript
            国产激情久久久久影院| 国产精品美女久久久| 国产精品女同一区二区久久| 亚洲精品国产成人99久久| 久久无码一区二区三区少妇| 国产成人精品久久| 狠狠色综合网站久久久久久久| 久久久久无码精品| 狠狠色婷婷久久一区二区三区 | 一本久久综合亚洲鲁鲁五月天亚洲欧美一区二区 | 久久亚洲精品国产精品| 久久久久久午夜成人影院| 色综合久久久久网| 伊人久久大香线蕉av不卡| 大美女久久久久久j久久| 久久狠狠爱亚洲综合影院| 国内精品久久久久久不卡影院| 国内精品人妻无码久久久影院导航 | 亚洲欧美成人久久综合中文网 | jizzjizz国产精品久久| 亚洲国产成人精品久久久国产成人一区二区三区综 | 嫩草伊人久久精品少妇AV| 91精品国产色综久久| 久久精品人人做人人爽97| 亚洲精品美女久久久久99小说| 国产精品一久久香蕉产线看| 久久久久久久女国产乱让韩| 99久久www免费人成精品| 国产成人精品免费久久久久| 天天躁日日躁狠狠久久| 亚洲婷婷国产精品电影人久久| 国产亚洲色婷婷久久99精品91| 久久亚洲国产欧洲精品一 | 日本欧美国产精品第一页久久| 91精品观看91久久久久久| 久久夜色精品国产亚洲| 99久久精品国产免看国产一区| 99re久久精品国产首页2020| 精品永久久福利一区二区| 国产精品一区二区久久| 91精品国产91久久久久久蜜臀|