【C++ grammar】抽象、封装与this指针
目錄
- 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)
將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)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: win7系统为什么有的玩不了地下城与勇士
- 下一篇: 大麦盒子为什么要收钱