【C语言】C语言实现面向对象编程之多态
00. 目錄
文章目錄
- 00. 目錄
- 01. 概述
- 02. C語言基于對象編程實(shí)現(xiàn)部分多態(tài)功能
- 03. 總結(jié)
- 04. 下載
- 05. 附錄
01. 概述
在C語言中還可以實(shí)現(xiàn)更深入的面向?qū)ο缶幊潭鄳B(tài)特性。例如:使用接口(interface)包含多個(gè)指向函數(shù)的指針,這樣就可以實(shí)現(xiàn)操作的"多態(tài)性"。
在面向?qū)ο笳Z言C++的實(shí)現(xiàn)上,使用了虛函數(shù)的方式,虛函數(shù)實(shí)質(zhì)上也是指向虛表(virtual table)的一個(gè)函數(shù)指針。C++虛表方式的本質(zhì)和包含的各個(gè)函數(shù)指針的操作數(shù)據(jù)結(jié)構(gòu)類似。
02. C語言基于對象編程實(shí)現(xiàn)部分多態(tài)功能
test.h文件內(nèi)容如下:
#ifndef __TEST_H__ #define __TEST_H__#ifdef __cplusplus //表示是C語言的頭文件 extern "C" { #endiftypedef void * HPERSON;//創(chuàng)建對象 HPERSON createPerson(const char *name);//顯示對象 void displayPerson(HPERSON person);//刪除對象 void deletePerson(HPERSON person);//-----------------STUDENT----------------- typedef void* HSTUDENT;//創(chuàng)建對象 HSTUDENT createStudent(const char *name, int age, int id, int score);//刪除對象 void deleteStudent(HSTUDENT student);#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>//函數(shù)指針 typedef struct _PersonOps {void (*display)(HPERSON); }PersonOps;//Person表示HPERSON句柄指向的結(jié)構(gòu)體 typedef struct _Person {//使用指針char *name;int age;int id;PersonOps* ops; }Person;//顯示對象 static void do_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); }static PersonOps opsPerson = {do_displayPerson };//創(chuàng)建對象 HPERSON createPerson(const char * name) {Person *p = NULL;printf("創(chuàng)建對象\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;p->ops = &opsPerson;return p; }//顯示對象 void displayPerson(HPERSON person) {Person* p = person;p->ops->display(person); }//刪除對象 void deletePerson(HPERSON person) {Person *p = person;if (NULL == person){return;}if (NULL != p->name){free(p->name);}free(person); }//---------------------STUDENT--------------------typedef struct _stu_t {Person person;int score; }Student;static void do_displayStudent(HSTUDENT student) {Student* s = student;if (NULL == student){return;}do_displayPerson(&(s->person));printf("score: %d\n", s->score); }static PersonOps opsStudent = { do_displayStudent };//創(chuàng)建對象 HSTUDENT createStudent(const char* name, int age, int id, int score) {Student *s = malloc(sizeof(Student));if (NULL == s){return NULL;}memset(s, 0, sizeof(Student));s->person.name = malloc(strlen(name) + 1);if (NULL == s->person.name){return;}memset(s->person.name, 0, strlen(name) + 1);strcpy(s->person.name, name);s->person.age = age;s->person.id = id;s->score = score;s->person.ops = &opsStudent;return s; }//刪除對象 void deleteStudent(HSTUDENT student) {Student *s = student;if (NULL == s){return;}if (NULL != s->person.name){free(s->person.name);}free(s);}main.c實(shí)現(xiàn)如下:
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<string.h> #include<stdlib.h> #include "test.h"//主函數(shù) int main() {HSTUDENT s = createStudent("李明", 12, 1, 99);displayPerson(s);deleteStudent(s);system("pause");return 0; }測試結(jié)果如下:
Name: 李明 age: 12 id:1 score: 99 請按任意鍵繼續(xù). . .03. 總結(jié)
在本例中,含有不同操作的結(jié)構(gòu)體內(nèi)存格式如下所示
PersonOpt數(shù)據(jù)結(jié)構(gòu)是一個(gè)表示"對象"操作的函數(shù)表。該程序中它包含了一個(gè)操作函數(shù)(函數(shù)指針)display。在實(shí)際應(yīng)用中,這種函數(shù)表數(shù)據(jù)結(jié)構(gòu)還可能包含更多的數(shù)據(jù)結(jié)構(gòu)。
總之,利用操作的函數(shù)指針列表可以實(shí)現(xiàn)部分“多態(tài)”功能,其本質(zhì)是對于不同類型的數(shù)據(jù)結(jié)構(gòu)調(diào)用不同的實(shí)現(xiàn)函數(shù)。
04. 下載
4.1 代碼下載:C語言實(shí)現(xiàn)對象編程之多態(tài)代碼.rar
05. 附錄
總結(jié)
以上是生活随笔為你收集整理的【C语言】C语言实现面向对象编程之多态的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Tiny4412】制作最小文件系统脚本
- 下一篇: 【ARM】Programmers Mod