博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C语言实现的OOP
阅读量:6409 次
发布时间:2019-06-23

本文共 3011 字,大约阅读时间需要 10 分钟。

我倒不是为了OOP而OOP,实在是OOP的一些特征,例如封装,多态其实是软件工程思想,这些思想不分语言,遵循了这些思想可以使得程序更有弹性,更易修改和维护,避免僵化,脆弱


shape.h 该文件定义的是图形接口,所有具体图形都必须实现自己计算面积,周长等API

#ifndef SHAPE_H#define SHAPE_Htypedef struct shape_t {    void *shapeData;    void (*area)(void *);    void (*release)(void *);}Shape;#endif

circle.h 圆形接口

#ifndef CIRCLE_H#define CIRCLE_Htypedef struct{    double r;}CircleData;typedef struct {    void *shapeData;    void (*area)(void *);    void (*release)(void *);}Circle;Circle *makeCircle(double r);#endif

circle.c 圆形的实现代码,static修饰的函数其实相当于private函数

#include 
#include
#include
#include "circle.h"const double PI = 3.14159;static void area(void *shape){ Circle *_circle = (Circle *)shape; CircleData* data = (CircleData*)_circle->shapeData; printf("the circle area is %f \n", data->r * data->r * PI);}static void release(void *shape){ Circle *_circle = (Circle *)shape; CircleData* data = (CircleData*)_circle->shapeData; free(data); free(_circle);}Circle *makeCircle(double r){ CircleData* circleData = (CircleData*)malloc(sizeof(CircleData)); Circle* circle = (Circle*)malloc(sizeof(Circle)); assert(circleData != NULL); assert(circle != NULL); assert(r > 0); circleData->r = r; circle->shapeData = circleData; circle->area = &area; circle->release = &release; return circle;}

square.h

#ifndef SQUARE_H#define SQUARE_Htypedef struct{    double x;    double y;}SquareData;typedef struct {    void *shapeData;    void (*area)(void *);    void (*release)(void *);}Square;Square *makeSquare(double x, double y);#endif

square.c

#include 
#include
#include
#include "square.h"static void area(void *shape){ Square *square = (Square *)shape; SquareData* data = (SquareData*)square->shapeData; printf("the square area is %f \n", data->x * data->y);}static void release(void *shape){ Square *square = (Square *)shape; SquareData* data = (SquareData*)square->shapeData; free(data); free(square);}Square *makeSquare(double x, double y){ SquareData* squareData = (SquareData*)malloc(sizeof(SquareData)); Square* square = (Square*)malloc(sizeof(Square)); assert(squareData != NULL); assert(square != NULL); assert(x > 0 && y > 0); squareData->x = x; squareData->y = y; square->shapeData = squareData; square->area = &area; square->release = &release; return square;}

main.c 所有的工作,都为了它,是为了让它的代码稳定

#include 
#include "shape.h"#include "circle.h"#include "square.h"void printShapeArea(Shape **shape,int length){ int i=0; for(i=0;i
area(shape[i]); shape[i]->release(shape[i]); }}int main(){ Shape *p[3] = {(Shape*)makeCircle(3.2),(Shape*)makeCircle(3.2),(Shape*)makeSquare(3.1,4)}; printShapeArea(p,3); return 0;}

 

总结:

printShapeArea 函数并不知道传入的图形列表分别都是哪些图形,这些图形又都怎么计算面积和周长,它唯一知道的是,这些图形计算面积和周长的接口是什么,通过这个接口计算就好了

 

转载于:https://www.cnblogs.com/code-style/p/3218871.html

你可能感兴趣的文章
Django form表单
查看>>
CTYL-9.14(tomcat端口与阿里云安全组,域名与tomcat配置,域名与反向代理)
查看>>
Java 多线程相关问题记录
查看>>
LNMP架构介绍、MySQL安装、PHP安装、 Nginx介绍
查看>>
简单的Spark+Mysql整合开发
查看>>
阿里java面试经验大汇总(附阿里职位需求)
查看>>
Python全套零基础视频教程+软件2018最新编程视频!
查看>>
内存管理之1:x86段式内存管理与保护模式
查看>>
20180925上课截图
查看>>
IO输入/输出流的简单总结
查看>>
JavaScript之DOM-9 HTML DOM(HTML DOM概述、常用HTML DOM对象、HTML表单)
查看>>
技术成长之路(一)
查看>>
中国北方国际五金城硬件选型
查看>>
php.exe启动时提示缺少MVCR110.dall 64位 window系统 解决
查看>>
判断是否为数字方法
查看>>
[翻译] EF Core in Action 关于这本书
查看>>
js Uncaught TypeError: undefined is not a function
查看>>
数据库存储引擎
查看>>
[2019.2.13]BZOJ4318 OSU!
查看>>
版本号带两个小数点的,如何比较大小?( NSStringCompareOptions )
查看>>