www.久久久久|狼友网站av天堂|精品国产无码a片|一级av色欲av|91在线播放视频|亚洲无码主播在线|国产精品草久在线|明星AV网站在线|污污内射久久一区|婷婷综合视频网站

當前位置:首頁 > 公眾號精選 > strongerHuang
[導讀]不知道有多少人去了解過語言的發(fā)展史,早期C語言的語法功能其實比較簡單。隨著應(yīng)用需求和場景的變化,C語言的語法功能在不斷升級變化。


編排 |?strongerHuang 微信公眾號 | strongerHuang

不知道有多少人去了解過語言的發(fā)展史,早期C語言語法功能其實比較簡單。隨著應(yīng)用需求和場景的變化,C語言的語法功能在不斷升級變化。


雖然我們的教材有這么一個結(jié)論:C語言是面向過程的語言,C++是面向?qū)ο?/span>編程語言,但面向?qū)ο蟮母拍钍窃贑語言階段就有了,而且應(yīng)用到了很多地方,比如某些操作系統(tǒng)內(nèi)核、通信協(xié)議等。


面向?qū)ο缶幊?,也就是大家說的OOP(Object Oriented Programming)并不是一種特定的語言或者工具,它只是一種設(shè)計方法、設(shè)計思想,它表現(xiàn)出來的三個最基本的特性就是封裝、繼承與多態(tài)。


1

為什么要用C語言實現(xiàn)面向?qū)ο?/span>


閱讀文本之前肯定有讀者會問這樣的問題:我們有C++面向?qū)ο蟮恼Z言,為什么還要用C語言實現(xiàn)面向?qū)ο竽兀?/span>


C語言這種非面向?qū)ο蟮恼Z言,同樣也可以使用面向?qū)ο蟮乃悸穪砭帉懗绦虻摹?strong>只是用面向?qū)ο蟮?strong>C++語言來實現(xiàn)面向?qū)ο缶幊虝唵我恍?,但是C語言的高效性是其他面向?qū)ο缶幊陶Z言無法比擬的。


當然使用C語言來實現(xiàn)面向?qū)ο蟮拈_發(fā)相對不容易理解,這就是為什么大多數(shù)人學過C語言卻看不懂Linux內(nèi)核源碼。


所以這個問題其實很好理解,只要有一定C語言編程經(jīng)驗的讀者都應(yīng)該能明白:面向過程的C語言和面向?qū)ο蟮腃++語言相比,代碼運行效率、代碼量都有很大差異。在性能不是很好、資源不是很多的MCU中使用C語言面向?qū)ο缶幊叹惋@得尤為重要。


2

具備條件


要想使用C語言實現(xiàn)面向?qū)ο螅紫刃枰邆湟恍┗A(chǔ)知識。比如:(C語言中的)結(jié)構(gòu)體、函數(shù)、指針,以及函數(shù)指針等,(C++中的)基類、派生、多態(tài)、繼承等。


首先,不僅僅是了解這些基礎(chǔ)知識,而是有一定的編程經(jīng)驗,因為上面說了“面向?qū)ο笫且环N設(shè)計方法、設(shè)計思想”,如果只是停留在字面意思的理解,沒有這種設(shè)計思想肯定不行。


因此,不建議初學者使用C語言實現(xiàn)面向?qū)ο?,特別是在真正項目中。建議把基本功練好,再使用。


利用C語言實現(xiàn)面向?qū)ο蟮姆椒ê芏?,下面就來描述最基本的封裝、繼承和多態(tài)。


3

封裝


封裝就是把數(shù)據(jù)和函數(shù)打包到一個類里面,其實大部分C語言編程者都已近接觸過了。


C 標準庫中的 fopen(), fclose(), fread(), fwrite()等函數(shù)的操作對象就是 FILE。數(shù)據(jù)內(nèi)容就是 FILE,數(shù)據(jù)的讀寫操作就是 fread()、fwrite(),fopen() 類比于構(gòu)造函數(shù),fclose() 就是析構(gòu)函數(shù)。


這個看起來似乎很好理解,那下面我們實現(xiàn)一下基本的封裝特性。


#ifndef SHAPE_H#define SHAPE_H #include  // Shape 的屬性typedef struct { int16_t x;  int16_t y; } Shape; // Shape 的操作函數(shù),接口函數(shù)void Shape_ctor(Shape * const me, int16_t x, int16_t y);void Shape_moveBy(Shape * const me, int16_t dx, int16_t dy);int16_t Shape_getX(Shape const * const me);int16_t Shape_getY(Shape const * const me); #endif /* SHAPE_H */


這是 Shape 類的聲明,非常簡單,很好理解。一般會把聲明放到頭文件里面 “Shape.h”。來看下 Shape 類相關(guān)的定義,當然是在 “Shape.c” 里面。
#include "shape.h" // 構(gòu)造函數(shù)void Shape_ctor(Shape * const me, int16_t x, int16_t y){ me->x = x; me->y = y;} void Shape_moveBy(Shape * const me, int16_t dx, int16_t dy) { me->x += dx; me->y += dy;} // 獲取屬性值函數(shù)int16_t Shape_getX(Shape const * const me) { return me->x;}int16_t Shape_getY(Shape const * const me) { return me->y;}

再看下 main.c
#include "shape.h" /* Shape class interface */#include  /* for printf() */ int main() { Shape s1, s2; /* multiple instances of Shape */  Shape_ctor(&s1, 0, 1); Shape_ctor(&s2, -1, 2);  printf("Shape s1(x=%d,y=%d)\n", Shape_getX(&s1), Shape_getY(&s1)); printf("Shape s2(x=%d,y=%d)\n", Shape_getX(&s2), Shape_getY(&s2));  Shape_moveBy(&s1, 2, -4); Shape_moveBy(&s2, 1, -2);  printf("Shape s1(x=%d,y=%d)\n", Shape_getX(&s1), Shape_getY(&s1)); printf("Shape s2(x=%d,y=%d)\n", Shape_getX(&s2), Shape_getY(&s2));  return 0;}

編譯之后,看看執(zhí)行結(jié)果:
Shape s1(x=0,y=1)Shape s2(x=-1,y=2)Shape s1(x=2,y=-3)Shape s2(x=0,y=0)

整個例子,非常簡單,非常好理解。以后寫代碼時候,要多去想想標準庫的文件IO操作,這樣也有意識的去培養(yǎng)面向?qū)ο缶幊痰乃季S。


4

繼承


繼承就是基于現(xiàn)有的一個類去定義一個新類,這樣有助于重用代碼,更好的組織代碼。在 C 語言里面,去實現(xiàn)單繼承也非常簡單,只要把基類放到繼承類的第一個數(shù)據(jù)成員的位置就行了。


例如,我們現(xiàn)在要創(chuàng)建一個 Rectangle 類,我們只要繼承 Shape 類已經(jīng)存在的屬性和操作,再添加不同于 Shape 的屬性和操作到 Rectangle 中。


下面是 Rectangle 的聲明與定義:

#ifndef RECT_H#define RECT_H #include "shape.h" // 基類接口 // 矩形的屬性typedef struct { Shape super; // 繼承 Shape  // 自己的屬性 uint16_t width; uint16_t height;} Rectangle; // 構(gòu)造函數(shù)void Rectangle_ctor(Rectangle * const me, int16_t x, int16_t y, uint16_t width, uint16_t height); #endif /* RECT_H */


#include "rect.h" // 構(gòu)造函數(shù)void Rectangle_ctor(Rectangle * const me, int16_t x, int16_t y, uint16_t width, uint16_t height){ /* first call superclass’ ctor */ Shape_ctor(&me->super, x, y);  /* next, you initialize the attributes added by this subclass... */ me->width = width; me->height = height;}


我們來看一下 Rectangle 的繼承關(guān)系和內(nèi)存布局:


因為有這樣的內(nèi)存布局,所以你可以很安全的傳一個指向 Rectangle 對象的指針到一個期望傳入 Shape 對象的指針的函數(shù)中,就是一個函數(shù)的參數(shù)是 “Shape *”,你可以傳入 “Rectangle *”,并且這是非常安全的。這樣的話,基類的所有屬性和方法都可以被繼承類繼承!


#include "rect.h" #include   int main() { Rectangle r1, r2;  // 實例化對象 Rectangle_ctor(&r1, 0, 2, 10, 15); Rectangle_ctor(&r2, -1, 3, 5, 8);  printf("Rect r1(x=%d,y=%d,width=%d,height=%d)\n", Shape_getX(&r1.super), Shape_getY(&r1.super), r1.width, r1.height); printf("Rect r2(x=%d,y=%d,width=%d,height=%d)\n", Shape_getX(&r2.super), Shape_getY(&r2.super), r2.width, r2.height);  // 注意,這里有兩種方式,一是強轉(zhuǎn)類型,二是直接使用成員地址 Shape_moveBy((Shape *)&r1, -2, 3); Shape_moveBy(&r2.super, 2, -1);  printf("Rect r1(x=%d,y=%d,width=%d,height=%d)\n", Shape_getX(&r1.super), Shape_getY(&r1.super), r1.width, r1.height); printf("Rect r2(x=%d,y=%d,width=%d,height=%d)\n", Shape_getX(&r2.super), Shape_getY(&r2.super), r2.width, r2.height);  return 0;}


輸出結(jié)果:

Rect r1(x=0,y=2,width=10,height=15)Rect r2(x=-1,y=3,width=5,height=8)Rect r1(x=-2,y=5,width=10,height=15)Rect r2(x=1,y=2,width=5,height=8)


5

多態(tài)


C++ 語言實現(xiàn)多態(tài)就是使用虛函數(shù)。在 C 語言里面,也可以實現(xiàn)多態(tài)。
現(xiàn)在,我們又要增加一個圓形,并且在 Shape 要擴展功能,我們要增加 area() 和 draw() 函數(shù)。但是 Shape 相當于抽象類,不知道怎么去計算自己的面積,更不知道怎么去畫出來自己。而且,矩形和圓形的面積計算方式和幾何圖像也是不一樣的。
下面讓我們重新聲明一下 Shape 類:
#ifndef SHAPE_H#define SHAPE_H #include  struct ShapeVtbl;// Shape 的屬性typedef struct { struct ShapeVtbl const *vptr; int16_t x;  int16_t y; } Shape; // Shape 的虛表struct ShapeVtbl { uint32_t (*area)(Shape const * const me); void (*draw)(Shape const * const me);}; // Shape 的操作函數(shù),接口函數(shù)void Shape_ctor(Shape * const me, int16_t x, int16_t y);void Shape_moveBy(Shape * const me, int16_t dx, int16_t dy);int16_t Shape_getX(Shape const * const me);int16_t Shape_getY(Shape const * const me); static inline uint32_t Shape_area(Shape const * const me) { return (*me->vptr->area)(me);} static inline void Shape_draw(Shape const * const me){ (*me->vptr->draw)(me);}  Shape const *largestShape(Shape const *shapes[], uint32_t nShapes);void drawAllShapes(Shape const *shapes[], uint32_t nShapes); #endif /*?SHAPE_H?*/


看下加上虛函數(shù)之后的類關(guān)系圖:


5.1 虛表和虛指針 虛表(Virtual Table)是這個類所有虛函數(shù)的函數(shù)指針的集合。
虛指針(Virtual Pointer)是一個指向虛表的指針。這個虛指針必須存在于每個對象實例中,會被所有子類繼承。
在《Inside The C++ Object Model》的第一章內(nèi)容中,有這些介紹。
5.2 在構(gòu)造函數(shù)中設(shè)置vptr 在每一個對象實例中,vptr 必須被初始化指向其 vtbl。最好的初始化位置就是在類的構(gòu)造函數(shù)中。事實上,在構(gòu)造函數(shù)中,C++ 編譯器隱式的創(chuàng)建了一個初始化的vptr。在 C 語言里面, 我們必須顯示的初始化vptr。
下面就展示一下,在 Shape 的構(gòu)造函數(shù)里面,如何去初始化這個 vptr。
#include "shape.h"#include  // Shape 的虛函數(shù)static uint32_t Shape_area_(Shape const * const me);static void Shape_draw_(Shape const * const me); // 構(gòu)造函數(shù)void Shape_ctor(Shape * const me, int16_t x, int16_t y) { // Shape 類的虛表 static struct ShapeVtbl const vtbl =  {  &Shape_area_, &Shape_draw_ }; me->vptr = &vtbl;  me->x = x; me->y = y;}  void Shape_moveBy(Shape * const me, int16_t dx, int16_t dy){ me->x += dx; me->y += dy;}  int16_t Shape_getX(Shape const * const me) { return me->x;}int16_t Shape_getY(Shape const * const me) { return me->y;} // Shape 類的虛函數(shù)實現(xiàn)static uint32_t Shape_area_(Shape const * const me) { assert(0); // 類似純虛函數(shù) return 0U; // 避免警告} static void Shape_draw_(Shape const * const me) { assert(0); // 純虛函數(shù)不能被調(diào)用}  Shape const *largestShape(Shape const *shapes[], uint32_t nShapes) { Shape const *s = (Shape *)0; uint32_t max = 0U; uint32_t i; for (i = 0U; i < nShapes; ++i)  { uint32_t area = Shape_area(shapes[i]);// 虛函數(shù)調(diào)用 if (area > max)  { max = area; s = shapes[i]; } } return s;}  void drawAllShapes(Shape const *shapes[], uint32_t nShapes) { uint32_t i; for (i = 0U; i < nShapes; ++i)  { Shape_draw(shapes[i]); // 虛函數(shù)調(diào)用 }}

5.3 繼承 vtbl 和 重載 vptr 上面已經(jīng)提到過,基類包含 vptr,子類會自動繼承。但是,vptr 需要被子類的虛表重新賦值。并且,這也必須發(fā)生在子類的構(gòu)造函數(shù)中。下面是 Rectangle 的構(gòu)造函數(shù)。
#include "rect.h" #include   // Rectangle 虛函數(shù)static uint32_t Rectangle_area_(Shape const * const me);static void Rectangle_draw_(Shape const * const me); // 構(gòu)造函數(shù)void Rectangle_ctor(Rectangle * const me, int16_t x, int16_t y, uint16_t width, uint16_t height){ static struct ShapeVtbl const vtbl =  { &Rectangle_area_, &Rectangle_draw_ }; Shape_ctor(&me->super, x, y); // 調(diào)用基類的構(gòu)造函數(shù) me->super.vptr = &vtbl; // 重載 vptr me->width = width; me->height = height;} // Rectangle's 虛函數(shù)實現(xiàn)static uint32_t Rectangle_area_(Shape const * const me) { Rectangle const * const me_ = (Rectangle const *)me; //顯示的轉(zhuǎn)換 return (uint32_t)me_->width * (uint32_t)me_->height;} static void Rectangle_draw_(Shape const * const me) { Rectangle const * const me_ = (Rectangle const *)me; //顯示的轉(zhuǎn)換 printf("Rectangle_draw_(x=%d,y=%d,width=%d,height=%d)\n", Shape_getX(me), Shape_getY(me), me_->width, me_->height);}


5.4 虛函數(shù)調(diào)用 有了前面虛表(Virtual Tables)和虛指針(Virtual Pointers)的基礎(chǔ)實現(xiàn),虛擬調(diào)用(后期綁定)就可以用下面代碼實現(xiàn)了。
uint32_t Shape_area(Shape const * const me){ return (*me->vptr->area)(me);}

這個函數(shù)可以放到.c文件里面,但是會帶來一個缺點就是每個虛擬調(diào)用都有額外的調(diào)用開銷。為了避免這個缺點,如果編譯器支持內(nèi)聯(lián)函數(shù)(C99)。我們可以把定義放到頭文件里面,類似下面:
static inline uint32_t Shape_area(Shape const * const me) { return (*me->vptr->area)(me);}

如果是老一點的編譯器(C89),我們可以用宏函數(shù)來實現(xiàn),類似下面這樣:
#define Shape_area(me_) ((*(me_)->vptr->area)((me_)))

看一下例子中的調(diào)用機制:
5.5 main.c
#include "rect.h" #include "circle.h" #include   int main() { Rectangle r1, r2;  Circle c1, c2;  Shape const *shapes[] =  {  &c1.super, &r2.super, &c2.super, &r1.super }; Shape const *s;  // 實例化矩形對象 Rectangle_ctor(&r1, 0, 2, 10, 15); Rectangle_ctor(&r2, -1, 3, 5, 8);  // 實例化圓形對象 Circle_ctor(&c1, 1, -2, 12); Circle_ctor(&c2, 1, -3, 6);  s = largestShape(shapes, sizeof(shapes)/sizeof(shapes[0])); printf("largetsShape s(x=%d,y=%d)\n", Shape_getX(s), Shape_getY(s));  drawAllShapes(shapes, sizeof(shapes)/sizeof(shapes[0]));  return 0;}
輸出結(jié)果:
largetsShape s(x=1,y=-2)Circle_draw_(x=1,y=-2,rad=12)Rectangle_draw_(x=-1,y=3,width=5,height=8)Circle_draw_(x=1,y=-3,rad=6)Rectangle_draw_(x=0,y=2,width=10,height=15)


6

總結(jié)


還是那句話,面向?qū)ο缶幊淌且环N方法,并不局限于某一種編程語言。用 C 語言實現(xiàn)封裝、單繼承,理解和實現(xiàn)起來比較簡單,多態(tài)反而會稍微復(fù)雜一點,如果打算廣泛的使用多態(tài),還是推薦轉(zhuǎn)到 C++ 語言上,畢竟這層復(fù)雜性被這個語言給封裝了,你只需要簡單的使用就行了。但并不代表,C 語言實現(xiàn)不了多態(tài)這個特性。

參考素材:

https://blog.csdn.net/onlyshi/article/details/81672279


------------ END ------------

推薦閱讀:
什么是自由軟件?
幾種特殊的函數(shù)宏封裝方式
無MMU搶占式操作系統(tǒng)的搶占工作原理
關(guān)注 微信公眾號『strongerHuang』,后臺回復(fù)“1024”查看更多內(nèi)容,回復(fù)“加群”按規(guī)則加入技術(shù)交流群。


長按前往圖中包含的公眾號關(guān)注


點擊“ 閱讀原文 ”查看更多分享,歡迎點分享、收藏、點贊、在看。

免責聲明:本文內(nèi)容由21ic獲得授權(quán)后發(fā)布,版權(quán)歸原作者所有,本平臺僅提供信息存儲服務(wù)。文章僅代表作者個人觀點,不代表本平臺立場,如有問題,請聯(lián)系我們,謝謝!

本站聲明: 本文章由作者或相關(guān)機構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點,本站亦不保證或承諾內(nèi)容真實性等。需要轉(zhuǎn)載請聯(lián)系該專欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請及時聯(lián)系本站刪除。
換一批
延伸閱讀

9月2日消息,不造車的華為或?qū)⒋呱龈蟮莫毥谦F公司,隨著阿維塔和賽力斯的入局,華為引望愈發(fā)顯得引人矚目。

關(guān)鍵字: 阿維塔 塞力斯 華為

加利福尼亞州圣克拉拉縣2024年8月30日 /美通社/ -- 數(shù)字化轉(zhuǎn)型技術(shù)解決方案公司Trianz今天宣布,該公司與Amazon Web Services (AWS)簽訂了...

關(guān)鍵字: AWS AN BSP 數(shù)字化

倫敦2024年8月29日 /美通社/ -- 英國汽車技術(shù)公司SODA.Auto推出其旗艦產(chǎn)品SODA V,這是全球首款涵蓋汽車工程師從創(chuàng)意到認證的所有需求的工具,可用于創(chuàng)建軟件定義汽車。 SODA V工具的開發(fā)耗時1.5...

關(guān)鍵字: 汽車 人工智能 智能驅(qū)動 BSP

北京2024年8月28日 /美通社/ -- 越來越多用戶希望企業(yè)業(yè)務(wù)能7×24不間斷運行,同時企業(yè)卻面臨越來越多業(yè)務(wù)中斷的風險,如企業(yè)系統(tǒng)復(fù)雜性的增加,頻繁的功能更新和發(fā)布等。如何確保業(yè)務(wù)連續(xù)性,提升韌性,成...

關(guān)鍵字: 亞馬遜 解密 控制平面 BSP

8月30日消息,據(jù)媒體報道,騰訊和網(wǎng)易近期正在縮減他們對日本游戲市場的投資。

關(guān)鍵字: 騰訊 編碼器 CPU

8月28日消息,今天上午,2024中國國際大數(shù)據(jù)產(chǎn)業(yè)博覽會開幕式在貴陽舉行,華為董事、質(zhì)量流程IT總裁陶景文發(fā)表了演講。

關(guān)鍵字: 華為 12nm EDA 半導體

8月28日消息,在2024中國國際大數(shù)據(jù)產(chǎn)業(yè)博覽會上,華為常務(wù)董事、華為云CEO張平安發(fā)表演講稱,數(shù)字世界的話語權(quán)最終是由生態(tài)的繁榮決定的。

關(guān)鍵字: 華為 12nm 手機 衛(wèi)星通信

要點: 有效應(yīng)對環(huán)境變化,經(jīng)營業(yè)績穩(wěn)中有升 落實提質(zhì)增效舉措,毛利潤率延續(xù)升勢 戰(zhàn)略布局成效顯著,戰(zhàn)新業(yè)務(wù)引領(lǐng)增長 以科技創(chuàng)新為引領(lǐng),提升企業(yè)核心競爭力 堅持高質(zhì)量發(fā)展策略,塑強核心競爭優(yōu)勢...

關(guān)鍵字: 通信 BSP 電信運營商 數(shù)字經(jīng)濟

北京2024年8月27日 /美通社/ -- 8月21日,由中央廣播電視總臺與中國電影電視技術(shù)學會聯(lián)合牽頭組建的NVI技術(shù)創(chuàng)新聯(lián)盟在BIRTV2024超高清全產(chǎn)業(yè)鏈發(fā)展研討會上宣布正式成立。 活動現(xiàn)場 NVI技術(shù)創(chuàng)新聯(lián)...

關(guān)鍵字: VI 傳輸協(xié)議 音頻 BSP

北京2024年8月27日 /美通社/ -- 在8月23日舉辦的2024年長三角生態(tài)綠色一體化發(fā)展示范區(qū)聯(lián)合招商會上,軟通動力信息技術(shù)(集團)股份有限公司(以下簡稱"軟通動力")與長三角投資(上海)有限...

關(guān)鍵字: BSP 信息技術(shù)
關(guān)閉
關(guān)閉