目錄
- 1、Abstraction and Encapsulation(抽象與封裝)
- 1. Data Field Encapsulation (數據域封裝)
- 2. Accessor and Mutator (訪問器與更改器)
- 2.1. To read/write private data, we need get/set function (為讀寫私有數據,需要get/set函數)
- 2.2. Signature of get function (General form) (get函數的一般原型)
- 2.3. Signature of get function (Bool type) (布爾型get函數的原型)
- 2.4. Signature of set function (set函數的原型)
- 3. Class Abstraction and Encapsulation (類抽象與封裝)
- 3.1. Class abstraction (類抽象)
- 3.2. Class encapsulation (類封裝)
- 3.3. 總結
- 2、The Scope of Members & "this" pointer(成員作用域與this指針)
- 1. The Scope of Data Members in Class (數據成員的作用域)
- 2. Hidden by same name (同名屏蔽)
- 3. The this Pointer (this指針)
- 4. Simple way to avoid name hidden (避免重名屏蔽的簡單方法)這是一種比this指針更加簡單的方法
- 5. 編碼規范
- 6、需要注意的地方
1、Abstraction and Encapsulation(抽象與封裝)
1. Data Field Encapsulation (數據域封裝)
數據域采用public的形式有2個問題
(1) First, data may be tampered. ( 數據會被類外的方法篡改)
(2) Second, it makes the class difficult to maintain and vulnerable to bugs. ( 使得類難于維護,易出現bug)
class Circle {
public:double radius;//……
};
// main() {circle1.radius=5; //類外代碼可修改public數據
將radius放入私有區域進行封裝,使得從外部不能直接訪問radius。
如果我們想對radius進行賦值或者讀取radius就需要使用到訪問器與更改器。
2. Accessor and Mutator (訪問器與更改器)
2.1. To read/write private data, we need get/set function (為讀寫私有數據,需要get/set函數)
(1) get function is referred to as a getter (獲取器,or accessor),
(2) set function is referred to as a setter (設置器,or mutator).
2.2. Signature of get function (General form) (get函數的一般原型)
returnType getPropertyName()
2.3. Signature of get function (Bool type) (布爾型get函數的原型)
bool isPropertyName()
2.4. Signature of set function (set函數的原型)
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 ( 在研究對象或系統時,為了更加專注于感興趣的細節,去除對象或系統的物理或時空細節/ 屬性的過程叫做抽象)
3.2. Class encapsulation (類封裝)
A language mechanism for restricting direct access to some of the object’s components.( 一種限制直接訪問對象組成部分的語言機制)
A language construct that facilitates the bundling of data with the methods (or other functions) operating on that data ( 一種實現數據和函數綁定的語言構造塊)
3.3. 總結
抽象: 提煉目標系統中我們關心的核心要素的過程
封裝: 綁定數據和函數的語言構造塊,以及限制訪問目標對象的內容的手段
2、The Scope of Members & “this” pointer(成員作用域與this指針)
1. The Scope of Data Members in Class (數據成員的作用域)
The data members are accessible to all constructors and functions in the class. (數據成員可被類內所有函數訪問)
Data fields and functions can be declared in any order in a class. (數據域與函數可按任意順序聲明)
2. Hidden by same name (同名屏蔽)
If a local variable has the same name as a data field: (若成員函數中的局部變量與某數據域同名)
(1) the local variable takes precedence ( 局部變量優先級高:就近原則)
(2) the data field with the same name is hidden. ( 同名數據域在函數中被屏蔽)
3. The this Pointer (this指針)
How do you reference a class’s hidden data field in a function? (如何在函數內訪問類中被屏蔽的數據域)? 可以使用 this 關鍵字
This 關鍵字的特性
(1) a special built-in pointer ( 特殊的內建指針)this指針不需要聲明也不需要賦初值。
(2) references to the calling object. ( 引用當前函數的調用對象)
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. 編碼規范
If the parameter of a member function has the same name as a private class variable, then the parameter should have underscore
suffix.若類的成員函數參數與私有成員變量名相同,那么參數名應加下劃線后綴
例:
class SomeClass {int length;
public:void setLength( int length_ );
6、需要注意的地方
1、我們不可以修改this指針的值,使之指向另外一個對象
2、this指針是自動初始化的、this指針指向調用當前函數的對象、我們不可以顯示地聲明this指針。