不知道有多少人去了解過語言的發(fā)展史,早期C語言的語法功能其實(shí)比較簡單。隨著應(yīng)用需求和場景的變化,C語言的語法功能在不斷升級變化。
雖然我們的教材有這么一個(gè)結(jié)論:C語言是面向過程的語言,C++是面向?qū)ο蟮?a target="_blank">編程語言,但面向?qū)ο蟮母拍钍窃贑語言階段就有了,而且應(yīng)用到了很多地方,比如某些操作系統(tǒng)內(nèi)核、通信協(xié)議等。
面向?qū)ο缶幊?,也就是大家說的OOP(Object Oriented Programming)并不是一種特定的語言或者工具,它只是一種設(shè)計(jì)方法、設(shè)計(jì)思想,它表現(xiàn)出來的三個(gè)最基本的特性就是封裝、繼承與多態(tài)。
strongerHuang
1
為什么要用C語言實(shí)現(xiàn)面向?qū)ο?/p>
閱讀文本之前肯定有讀者會(huì)問這樣的問題:我們有C++面向?qū)ο蟮恼Z言,為什么還要用C語言實(shí)現(xiàn)面向?qū)ο竽兀?/p>
C語言這種非面向?qū)ο蟮恼Z言,同樣也可以使用面向?qū)ο蟮乃悸穪砭帉懗绦虻?。只是用面向?qū)ο蟮腃++語言來實(shí)現(xiàn)面向?qū)ο缶幊虝?huì)更簡單一些,但是C語言的高效性是其他面向?qū)ο缶幊陶Z言無法比擬的。
當(dāng)然使用C語言來實(shí)現(xiàn)面向?qū)ο蟮拈_發(fā)相對不容易理解,這就是為什么大多數(shù)人學(xué)過C語言卻看不懂Linux內(nèi)核源碼。
所以這個(gè)問題其實(shí)很好理解,只要有一定C語言編程經(jīng)驗(yàn)的讀者都應(yīng)該能明白:面向過程的C語言和面向?qū)ο蟮腃++語言相比,代碼運(yùn)行效率、代碼量都有很大差異。在性能不是很好、資源不是很多的MCU中使用C語言面向?qū)ο缶幊叹惋@得尤為重要。
strongerHuang
2
具備條件
要想使用C語言實(shí)現(xiàn)面向?qū)ο?,首先需要具備一些基礎(chǔ)知識。比如:(C語言中的)結(jié)構(gòu)體、函數(shù)、指針,以及函數(shù)指針等,(C++中的)基類、派生、多態(tài)、繼承等。
首先,不僅僅是了解這些基礎(chǔ)知識,而是有一定的編程經(jīng)驗(yàn),因?yàn)樯厦嬲f了“面向?qū)ο笫且环N設(shè)計(jì)方法、設(shè)計(jì)思想”,如果只是停留在字面意思的理解,沒有這種設(shè)計(jì)思想肯定不行。
因此,不建議初學(xué)者使用C語言實(shí)現(xiàn)面向?qū)ο?,特別是在真正項(xiàng)目中。建議把基本功練好,再使用。
利用C語言實(shí)現(xiàn)面向?qū)ο蟮姆椒ê芏啵旅婢蛠砻枋鲎罨镜姆庋b、繼承和多態(tài)。
strongerHuang
3
封裝
封裝就是把數(shù)據(jù)和函數(shù)打包到一個(gè)類里面,其實(shí)大部分C語言編程者都已近接觸過了。
C 標(biāo)準(zhǔn)庫中的 fopen(), fclose(), fread(), fwrite()等函數(shù)的操作對象就是 FILE。數(shù)據(jù)內(nèi)容就是 FILE,數(shù)據(jù)的讀寫操作就是 fread()、fwrite(),fopen() 類比于構(gòu)造函數(shù),fclose() 就是析構(gòu)函數(shù)。
這個(gè)看起來似乎很好理解,那下面我們實(shí)現(xiàn)一下基本的封裝特性。
#ifndef SHAPE_H#define SHAPE_H #include
這是 Shape 類的聲明,非常簡單,很好理解。一般會(huì)把聲明放到頭文件里面 “Shape.h”。來看下 Shape 類相關(guān)的定義,當(dāng)然是在 “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
Shape s1(x=0,y=1)Shape s2(x=-1,y=2)Shape s1(x=2,y=-3)Shape s2(x=0,y=0)
整個(gè)例子,非常簡單,非常好理解。以后寫代碼時(shí)候,要多去想想標(biāo)準(zhǔn)庫的文件IO操作,這樣也有意識的去培養(yǎng)面向?qū)ο缶幊痰乃季S。
strongerHuang
4
繼承
繼承就是基于現(xiàn)有的一個(gè)類去定義一個(gè)新類,這樣有助于重用代碼,更好的組織代碼。在 C 語言里面,去實(shí)現(xiàn)單繼承也非常簡單,只要把基類放到繼承類的第一個(gè)數(shù)據(jù)成員的位置就行了。
例如,我們現(xiàn)在要?jiǎng)?chuàng)建一個(gè) 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)存布局:
因?yàn)橛羞@樣的內(nèi)存布局,所以你可以很安全的傳一個(gè)指向 Rectangle 對象的指針到一個(gè)期望傳入 Shape 對象的指針的函數(shù)中,就是一個(gè)函數(shù)的參數(shù)是 “Shape *”,你可以傳入 “Rectangle *”,并且這是非常安全的。這樣的話,基類的所有屬性和方法都可以被繼承類繼承!
#include "rect.h" #include
輸出結(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)
strongerHuang
5
多態(tài)
C++ 語言實(shí)現(xiàn)多態(tài)就是使用虛函數(shù)。在 C 語言里面,也可以實(shí)現(xiàn)多態(tài)。 現(xiàn)在,我們又要增加一個(gè)圓形,并且在 Shape 要擴(kuò)展功能,我們要增加 area() 和 draw() 函數(shù)。但是 Shape 相當(dāng)于抽象類,不知道怎么去計(jì)算自己的面積,更不知道怎么去畫出來自己。而且,矩形和圓形的面積計(jì)算方式和幾何圖像也是不一樣的。 下面讓我們重新聲明一下 Shape 類:
#ifndef SHAPE_H#define SHAPE_H #include
看下加上虛函數(shù)之后的類關(guān)系圖:
5.1 虛表和虛指針虛表(Virtual Table)是這個(gè)類所有虛函數(shù)的函數(shù)指針的集合。 虛指針(Virtual Pointer)是一個(gè)指向虛表的指針。這個(gè)虛指針必須存在于每個(gè)對象實(shí)例中,會(huì)被所有子類繼承。 在《Inside The C++ Object Model》的第一章內(nèi)容中,有這些介紹。 5.2 在構(gòu)造函數(shù)中設(shè)置vptr在每一個(gè)對象實(shí)例中,vptr 必須被初始化指向其 vtbl。最好的初始化位置就是在類的構(gòu)造函數(shù)中。事實(shí)上,在構(gòu)造函數(shù)中,C++ 編譯器隱式的創(chuàng)建了一個(gè)初始化的vptr。在 C 語言里面, 我們必須顯示的初始化vptr。 下面就展示一下,在 Shape 的構(gòu)造函數(shù)里面,如何去初始化這個(gè) vptr。
#include "shape.h"#include
#include "rect.h" #include
5.4 虛函數(shù)調(diào)用有了前面虛表(Virtual Tables)和虛指針(Virtual Pointers)的基礎(chǔ)實(shí)現(xiàn),虛擬調(diào)用(后期綁定)就可以用下面代碼實(shí)現(xiàn)了。
uint32_t Shape_area(Shape const * const me){ return (*me->vptr->area)(me);} 這個(gè)函數(shù)可以放到.c文件里面,但是會(huì)帶來一個(gè)缺點(diǎn)就是每個(gè)虛擬調(diào)用都有額外的調(diào)用開銷。為了避免這個(gè)缺點(diǎn),如果編譯器支持內(nèi)聯(lián)函數(shù)(C99)。我們可以把定義放到頭文件里面,類似下面:
static inline uint32_t Shape_area(Shape const * const me) { return (*me->vptr->area)(me);} 如果是老一點(diǎn)的編譯器(C89),我們可以用宏函數(shù)來實(shí)現(xiàn),類似下面這樣:
#define Shape_area(me_) ((*(me_)->vptr->area)((me_))) 看一下例子中的調(diào)用機(jī)制:
5.5 main.c
#include "rect.h" #include "circle.h" #include
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)
strongerHuang
6
總結(jié)
還是那句話,面向?qū)ο缶幊淌且环N方法,并不局限于某一種編程語言。用 C 語言實(shí)現(xiàn)封裝、單繼承,理解和實(shí)現(xiàn)起來比較簡單,多態(tài)反而會(huì)稍微復(fù)雜一點(diǎn),如果打算廣泛的使用多態(tài),還是推薦轉(zhuǎn)到 C++ 語言上,畢竟這層復(fù)雜性被這個(gè)語言給封裝了,你只需要簡單的使用就行了。但并不代表,C 語言實(shí)現(xiàn)不了多態(tài)這個(gè)特性。
參考素材:
https://blog.csdn.net/onlyshi/article/details/81672279
責(zé)任編輯:xj
原文標(biāo)題:C語言實(shí)現(xiàn)面向?qū)ο蟮脑?/p>
文章出處:【微信公眾號:strongerHuang】歡迎添加關(guān)注!文章轉(zhuǎn)載請注明出處。
-
C語言
+關(guān)注
關(guān)注
180文章
7575瀏覽量
134070 -
C++
+關(guān)注
關(guān)注
21文章
2085瀏覽量
73302 -
面向?qū)ο?/span>
+關(guān)注
關(guān)注
0文章
64瀏覽量
9961
原文標(biāo)題:C語言實(shí)現(xiàn)面向?qū)ο蟮脑?/p>
文章出處:【微信號:strongerHuang,微信公眾號:strongerHuang】歡迎添加關(guān)注!文章轉(zhuǎn)載請注明出處。
發(fā)布評論請先 登錄
相關(guān)推薦
評論