日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

【C++ grammar】抽象、封装与this指针

發(fā)布時間:2023/12/1 c/c++ 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【C++ grammar】抽象、封装与this指针 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

目錄

  • 1、Abstraction and Encapsulation(抽象與封裝)
    • 1. Data Field Encapsulation (數(shù)據(jù)域封裝)
    • 2. Accessor and Mutator (訪問器與更改器)
      • 2.1. To read/write private data, we need get/set function (為讀寫私有數(shù)據(jù),需要get/set函數(shù))
      • 2.2. Signature of get function (General form) (get函數(shù)的一般原型)
      • 2.3. Signature of get function (Bool type) (布爾型get函數(shù)的原型)
      • 2.4. Signature of set function (set函數(shù)的原型)
    • 3. Class Abstraction and Encapsulation (類抽象與封裝)
      • 3.1. Class abstraction (類抽象)
      • 3.2. Class encapsulation (類封裝)
      • 3.3. 總結(jié)
  • 2、The Scope of Members & "this" pointer(成員作用域與this指針)
    • 1. The Scope of Data Members in Class (數(shù)據(jù)成員的作用域)
    • 2. Hidden by same name (同名屏蔽)
    • 3. The this Pointer (this指針)
    • 4. Simple way to avoid name hidden (避免重名屏蔽的簡單方法)這是一種比this指針更加簡單的方法
    • 5. 編碼規(guī)范
    • 6、需要注意的地方

1、Abstraction and Encapsulation(抽象與封裝)

1. Data Field Encapsulation (數(shù)據(jù)域封裝)

數(shù)據(jù)域采用public的形式有2個問題
(1) First, data may be tampered. ( 數(shù)據(jù)會被類外的方法篡改)
(2) Second, it makes the class difficult to maintain and vulnerable to bugs. ( 使得類難于維護(hù),易出現(xiàn)bug)

class Circle { public:double radius;//…… }; // main() {circle1.radius=5; //類外代碼可修改public數(shù)據(jù)

將radius放入私有區(qū)域進(jìn)行封裝,使得從外部不能直接訪問radius。

如果我們想對radius進(jìn)行賦值或者讀取radius就需要使用到訪問器與更改器。

2. Accessor and Mutator (訪問器與更改器)

2.1. To read/write private data, we need get/set function (為讀寫私有數(shù)據(jù),需要get/set函數(shù))

(1) get function is referred to as a getter (獲取器,or accessor),
(2) set function is referred to as a setter (設(shè)置器,or mutator).

2.2. Signature of get function (General form) (get函數(shù)的一般原型)

returnType getPropertyName()

2.3. Signature of get function (Bool type) (布爾型get函數(shù)的原型)

bool isPropertyName()

2.4. Signature of set function (set函數(shù)的原型)

void setPropertyName(dataType propertyValue)、

3. Class Abstraction and Encapsulation (類抽象與封裝)

3.1. Class abstraction (類抽象)

The process of removing physical, spatial, or temporal details or attributes in the study of objects or systems in order to more closely attend to other details of interest ( 在研究對象或系統(tǒng)時,為了更加專注于感興趣的細(xì)節(jié),去除對象或系統(tǒng)的物理或時空細(xì)節(jié)/ 屬性的過程叫做抽象)

3.2. Class encapsulation (類封裝)

A language mechanism for restricting direct access to some of the object’s components.( 一種限制直接訪問對象組成部分的語言機(jī)制)
A language construct that facilitates the bundling of data with the methods (or other functions) operating on that data ( 一種實現(xiàn)數(shù)據(jù)和函數(shù)綁定的語言構(gòu)造塊)

3.3. 總結(jié)

抽象: 提煉目標(biāo)系統(tǒng)中我們關(guān)心的核心要素的過程
封裝: 綁定數(shù)據(jù)和函數(shù)的語言構(gòu)造塊,以及限制訪問目標(biāo)對象的內(nèi)容的手段

2、The Scope of Members & “this” pointer(成員作用域與this指針)

1. The Scope of Data Members in Class (數(shù)據(jù)成員的作用域)

The data members are accessible to all constructors and functions in the class. (數(shù)據(jù)成員可被類內(nèi)所有函數(shù)訪問)
Data fields and functions can be declared in any order in a class. (數(shù)據(jù)域與函數(shù)可按任意順序聲明)

2. Hidden by same name (同名屏蔽)

If a local variable has the same name as a data field: (若成員函數(shù)中的局部變量與某數(shù)據(jù)域同名)

(1) the local variable takes precedence ( 局部變量優(yōu)先級高:就近原則)
(2) the data field with the same name is hidden. ( 同名數(shù)據(jù)域在函數(shù)中被屏蔽)

3. The this Pointer (this指針)

How do you reference a class’s hidden data field in a function? (如何在函數(shù)內(nèi)訪問類中被屏蔽的數(shù)據(jù)域)? 可以使用 this 關(guān)鍵字
This 關(guān)鍵字的特性
(1) a special built-in pointer ( 特殊的內(nèi)建指針)this指針不需要聲明也不需要賦初值。
(2) references to the calling object. ( 引用當(dāng)前函數(shù)的調(diào)用對象)

4. Simple way to avoid name hidden (避免重名屏蔽的簡單方法)這是一種比this指針更加簡單的方法

class Circle { public:Circle();Circle(double radius_){//this->radius = radius;radius = radius_; } private:double radius; public:void setRadius(double);//……};

5. 編碼規(guī)范

  • If the parameter of a member function has the same name as a private class variable, then the parameter should have underscore
    suffix.

  • 若類的成員函數(shù)參數(shù)與私有成員變量名相同,那么參數(shù)名應(yīng)加下劃線后綴

  • 例:

    class SomeClass {int length; public:void setLength( int length_ );

    6、需要注意的地方

    1、我們不可以修改this指針的值,使之指向另外一個對象
    2、this指針是自動初始化的、this指針指向調(diào)用當(dāng)前函數(shù)的對象、我們不可以顯示地聲明this指針。

    總結(jié)

    以上是生活随笔為你收集整理的【C++ grammar】抽象、封装与this指针的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。

    主站蜘蛛池模板: 国产尤物av | 欧美永久精品 | 超碰520| 欧美黄色短片 | 国产成人无码www免费视频播放 | 亚洲一区二区色 | 日本午夜网| 波多野吉衣视频在线观看 | 亚洲经典久久 | brazzers欧美一区二区 | 亚洲欧美在线综合 | 中文字幕第2页 | 亚洲国产一区在线 | 欧亚一级片 | 你懂的在线视频网站 | 国产精品无码免费专区午夜 | 日本xx视频| 色屁屁www| 天天干天天操天天舔 | 91大神福利视频 | 日韩成人影视 | 自宅警备员在线观看 | 国产无套在线观看 | 欧美成人一区在线观看 | 色黄视频在线观看 | 精品久久久久久久中文字幕 | 欧美色图17p| 国产三级影院 | 中文字幕在线观看二区 | 女王人厕视频2ⅴk | 欧美aa大片 | 伊人青青久久 | 黄色成人在线网站 | 色老汉av一区二区三区 | 国产三级按摩推拿按摩 | 阿v天堂在线观看 | 成人精品久久 | 欧美日韩亚洲在线 | 第四色影音先锋 | 熟女熟妇伦久久影院毛片一区二区 | 高清国产mv在线观看 | 樱花影院电视剧免费 | 三级黄色免费网站 | 麻豆视频播放 | 亚洲视频在线一区 | 日本精品久久久久久 | missav | 免费高清av在线看 | 久久久久人妻精品色欧美 | 欧美熟妇精品一区二区蜜桃视频 | 亚洲www| 日本高清www免费视频 | 日韩美女av在线 | 男生和女生操操 | 久久精品成人一区二区三区蜜臀 | 国产精品永久免费观看 | 国产精品视频www | 无码国产精品一区二区免费16 | 男女做的视频 | 欧美天天色 | 亚洲第一中文字幕 | 亚洲午夜片 | 国 产 黄 色 大 片 | 91欧美日韩 | 成人在线综合网 | 成a人v | 中文字幕在线观看视频一区二区 | 久久精品国产一区二区 | 国产女优在线播放 | 99精品自拍| 久草热线 | 中字幕一区二区三区乱码 | 伊人国产精品 | 色狠av| 国产同性人妖ts口直男 | 精品无码一区二区三区蜜臀 | 欧美视频一区在线观看 | 色婷婷成人 | 亚洲国产精品毛片 | 精品国产伦一区二区三区 | 国产精品视频在线观看 | 99久久精品国产色欲 | 狠狠干天天爱 | 伊人久久五月天 | 香蕉视频首页 | 中文在线资源天堂 | 97超碰超碰 | 男女做爰真人视频直播 | 初高中福利视频网站 | 日本加勒比中文字幕 | 日韩欧美精品国产 | 男女日批视频 | 动漫羞羞 | 色一五月 | 国产成年人视频网站 | 欧美精品一区二 | av电影在线观看 | 国产又爽又黄免费软件 | www.成人免费 | 永久毛片 |