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

            常用鏈接

            留言簿(28)

            我參與的團(tuán)隊(duì)

            搜索

            •  

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            一、說(shuō)明

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

            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來(lái)標(biāo)明屬的讀寫函數(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

            在這里就會(huì) 打印出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了,又是做了大大的簡(jiǎn)化。

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

            只需要在類的成員方法前,加一個(gè)static關(guān)鍵字就可以了,如果同一個(gè)類的靜態(tài)函數(shù),可以用this來(lái)調(diào)用。如下面的astatcfun1調(diào)用astatcfun,也可以用類名.的方問(wèn),如astatcfun2調(diào)用astatcfun,建議還是用this,在使用的時(shí)候,靜態(tài)方法,不需new出對(duì)象來(lái),直接用對(duì)象.的方式,如下面的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"; //同一個(gè)類的靜態(tài)函數(shù)調(diào)用函數(shù),只需要用this.就可以
            }
            kkk() { return "kkk"; }
            ccc() {
            return a.aaa() + "ccc" + this.kkk(); //同一個(gè)類的非靜態(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的派生通過(guò)extends這個(gè)關(guān)鍵字就可以,如

            class b extends a() { };
            posted on 2017-07-17 11:47 冬瓜 閱讀(636) 評(píng)論(0)  編輯 收藏 引用 所屬分類: javascript
            亚洲国产一成人久久精品| 国内精品伊人久久久久av一坑| 韩国免费A级毛片久久| 国产成人精品久久一区二区三区 | 精品99久久aaa一级毛片| 久久精品国产亚洲一区二区三区| 久久综合给合综合久久| 久久久无码精品亚洲日韩按摩| 久久精品国产亚洲网站| 伊人伊成久久人综合网777| 99久久人妻无码精品系列蜜桃| 青青久久精品国产免费看| 国产午夜免费高清久久影院| 久久影院久久香蕉国产线看观看| 国产激情久久久久久熟女老人| 国产精久久一区二区三区| 久久久久人妻一区精品色| 久久精品综合网| 久久中文字幕一区二区| 久久综合综合久久综合| 久久久久久久久66精品片| 国产精品亚洲美女久久久| 99久久超碰中文字幕伊人| 无码精品久久久天天影视| 伊人久久精品影院| 久久人人爽人爽人人爽av| 国产精品青草久久久久福利99| AV无码久久久久不卡蜜桃| 国产Av激情久久无码天堂| 久久综合九色综合久99| 久久播电影网| 岛国搬运www久久| 久久美女网站免费| 久久精品草草草| 色综合久久88色综合天天| 精品久久久久久无码专区不卡| 色欲综合久久躁天天躁蜜桃| 天堂久久天堂AV色综合| 亚洲AV无码久久精品成人| 久久婷婷五月综合97色 | 亚洲&#228;v永久无码精品天堂久久|