代碼:C語言中的面向對象

編程語言 C語言 技術 宏思微想 宏思微想 2017-08-28

【宏思微想:科技生活,技術開發】

面向對象三大基本特徵:封裝(packaging),繼承(inherifance),多態(polymorphic)。

一個簡單的範例:

封裝:將屬性(變量)和操作(方法)封裝在一個結構體中。

struct _Data;

typdef void (*process)(struct _Data *pData);

typdef struct _Data

{

int Value;

process pProcess;

}Data;

繼承:一個結構體包含另外一個結構體的所有屬性和操作。

typdef struct _Parnet

{

int data_parent;

void (*pfun)(void);

}Parent;

typdef struct _Child

{

struct _Parent parent;

int data_child;

}Child;

多態:不同的調用可以發生多種形態。注意函數指針使用的是結構體的指針,用意在於方便訪問結構體成員變量,相當於C++類的this指針。

Typdef struct _Play

{

void *pData;

void (*pPlay)(struct _Play *pPlay);

}Play;

代碼:C語言中的面向對象

——————(完)——————

相關推薦

推薦中...