【C语言】C语言实现面向对象编程之封装
00. 目錄
文章目錄
- 00. 目錄
- 01. 前言
- 02. 簡單程序示例
- 03. 程序示例優(yōu)化
- 04. 總結(jié)
- 05. 參考
01. 前言
面向?qū)ο缶幊?/strong>(OOP)并不是一種特定的語言或者工具,它只是一種設(shè)計(jì)方法、設(shè)計(jì)思想。它表現(xiàn)出來的三個(gè)最基本的特性就是封裝、繼承與多態(tài)。很多面向?qū)ο蟮木幊陶Z言已經(jīng)包含這三個(gè)特性了,例如 Smalltalk、C++、Java。但是你也可以用幾乎所有的編程語言來實(shí)現(xiàn)面向?qū)ο缶幊?#xff0c;例如 ANSI-C。要記住,面向?qū)ο笫且环N思想,一種方法,不要太拘泥于編程語言。
C語言不是面向?qū)ο蟮恼Z言,因此使用C語言完全實(shí)現(xiàn)面向?qū)ο蟮木幊淌遣豢赡堋5鞘褂肅語言可以很容易實(shí)現(xiàn)基于對(duì)象的編程,并且基本實(shí)現(xiàn)對(duì)象的封裝性,部分實(shí)現(xiàn)對(duì)象的繼承。
02. 簡單程序示例
C語言實(shí)現(xiàn)對(duì)象封裝的小程序,包含三部分:接口,調(diào)用和實(shí)現(xiàn),均使用C語言的基本語法實(shí)現(xiàn)。
頭文件test.h聲明如下
#ifndef __TEST_H__ #define __TEST_H__#ifdef __cplusplus //表示是C語言的頭文件 extern "C" { #endiftypedef void * HPERSON;//創(chuàng)建對(duì)象 HPERSON createPerson(const char *name);//設(shè)置對(duì)象 void setPerson(HPERSON person, int age, int id);//顯示對(duì)象 void displayPerson(HPERSON person);//刪除對(duì)象 void deletePerson(HPERSON person);#ifdef __cplusplus } #endif#endif /*__TEST_H__*/test.c文件如下
#define _CRT_SECURE_NO_WARNINGS #include "test.h" #include <stdio.h> #include <stdlib.h> #include <string.h>#define NAMEMAX 32//Person表示HPERSON句柄指向的結(jié)構(gòu)體 typedef struct _Person {char name[NAMEMAX];int age;int id; }Person;//創(chuàng)建對(duì)象 HPERSON createPerson(const char * name) {Person *p = NULL;printf("創(chuàng)建對(duì)象\n");if (strlen(name) + 1 <= NAMEMAX){p = malloc(sizeof(Person));if (NULL == p){printf("分配內(nèi)存失敗\n");return NULL;}memset(p, 0, sizeof(Person));strcpy(p->name, name);p->age = 0;p->id = 0;}return p; }//設(shè)置對(duì)象 void setPerson(HPERSON person, int age, int id) {Person *p = person;if (NULL != p){p->age = age;p->id = id;} }//顯示對(duì)象 void displayPerson(HPERSON person) {Person *p = person;if (NULL == p){printf("displayPerson 參數(shù)非法\n");return;}printf("Name: %s age: %d id:%d\n", p->name, p->age, p->id); }//刪除對(duì)象 void deletePerson(HPERSON person) {free(person); }main.c實(shí)現(xiàn)如下
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<string.h> #include<stdlib.h> #include "test.h"int main() {HPERSON p = createPerson("LiMing");setPerson(p, 18, 1);displayPerson(p);deletePerson(p);system("pause");return 0; }測(cè)試結(jié)果
創(chuàng)建對(duì)象 Name: LiMing age: 18 id:1 請(qǐng)按任意鍵繼續(xù). . .總結(jié)
對(duì)象的操作一直使用句柄HPERSON,在調(diào)用的過程中看不到程序處理的細(xì)節(jié),比如對(duì)象的建立和釋放。接口掩蓋了實(shí)現(xiàn)的很多細(xì)節(jié),在接口中看不到指針的使用,并且不能夠也沒有必要訪問數(shù)據(jù)結(jié)構(gòu)。
03. 程序示例優(yōu)化
頭文件test.h聲明如下
#ifndef __TEST_H__ #define __TEST_H__#ifdef __cplusplus //表示是C語言的頭文件 extern "C" { #endiftypedef void * HPERSON;//創(chuàng)建對(duì)象 HPERSON createPerson(const char *name);//設(shè)置對(duì)象 void setPerson(HPERSON person, int age, int id);//顯示對(duì)象 void displayPerson(HPERSON person);//刪除對(duì)象 void deletePerson(HPERSON person);#ifdef __cplusplus } #endif#endif /*__TEST_H__*/test.c文件實(shí)現(xiàn)如下
#define _CRT_SECURE_NO_WARNINGS #include "test.h" #include <stdio.h> #include <stdlib.h> #include <string.h>//Person表示HPERSON句柄指向的結(jié)構(gòu)體 typedef struct _Person {//使用指針char *name;int age;int id; }Person;//創(chuàng)建對(duì)象 HPERSON createPerson(const char * name) {Person *p = NULL;printf("創(chuàng)建對(duì)象\n");p = malloc(sizeof(Person));if (NULL == p){printf("分配內(nèi)存失敗\n");return NULL;}memset(p, 0, sizeof(Person));p->name = malloc(strlen(name) + 1);if (NULL == p->name){printf("分配內(nèi)存失敗\n");return NULL;}strcpy(p->name, name);p->age = 0;p->id = 0;return p; }//設(shè)置對(duì)象 void setPerson(HPERSON person, int age, int id) {Person *p = person;if (NULL != p){p->age = age;p->id = id;} }//顯示對(duì)象 void displayPerson(HPERSON person) {Person *p = person;if (NULL == p){printf("displayPerson 參數(shù)非法\n");return;}printf("Name: %s age: %d id:%d\n", p->name, p->age, p->id); }//刪除對(duì)象 void deletePerson(HPERSON person) {Person *p = person;if (NULL == person){return;}if (NULL != p->name){free(p->name);}free(person); }main.c測(cè)試代碼
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<string.h> #include<stdlib.h> #include "test.h"int main() {HPERSON p = createPerson("LiMing");setPerson(p, 18, 1);displayPerson(p);deletePerson(p);system("pause");return 0; }測(cè)試結(jié)果
創(chuàng)建對(duì)象 Name: LiMing age: 18 id:1 請(qǐng)按任意鍵繼續(xù). . .04. 總結(jié)
基于對(duì)象的C語言編程的流程,程序從接口到實(shí)現(xiàn)都是使用C語言實(shí)現(xiàn)的。
注:圖片來源于嵌入式Linux上的C語言編程實(shí)踐
05. 參考
代碼:【C語言】C語言實(shí)現(xiàn)面向?qū)ο缶幊讨庋b代碼.rar
總結(jié)
以上是生活随笔為你收集整理的【C语言】C语言实现面向对象编程之封装的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Tools】Ubuntu18.04破解
- 下一篇: 【C语言】C语言实现面向对象编程之继承