日韩性视频-久久久蜜桃-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)容還不錯,歡迎將生活随笔推薦給好友。