纯C实现面向对象之接口编程
创建如下文件目录 :
Shape.h
#include <stdlib.h> //接口 #ifndef Interface #define Interface struct #endif //类 #ifndef Class #define Class struct #endif //SHAPE_H_ #ifndef SHAPE_H_ #define SHAPE_H_ //抽象形状类 Class Shape; typedef Class Shape * p_shape; //抽象形状类声明 Class Shape { int edge; int (*getEdge)(p_shape shape); int (*calcArea)(p_shape shape); }; //形状类构造函数 p_shape Shape(int edge); //形状类析构函数 void _Shape(void * shape); //形状类实例方法 int Shape_getEdge(p_shape shape); //形状类抽象方法,返回-1,表示未实现 int Shape_calcArea(p_shape shape); #endif /* SHAPE_H_ */
Shape.c
#include "Shape.h" p_shape Shape(int edge) { p_shape obj = (p_shape) malloc(sizeof(Class Shape)); obj->edge = edge; obj->getEdge = Shape_getEdge; obj->calcArea = Shape_calcArea; return obj; } void _Shape(void * obj) { if (NULL != obj) { free(obj); } } int Shape_getEdge(p_shape shape) { return shape->edge; } int Shape_calcArea(p_shape shape) { return -1; }
Triangle.h
#include "Shape.h" #ifndef TRIANGLE_H_ #define TRIANGLE_H_ //三角形类 Class Triangle; typedef Class Triangle * p_triangle; //三角形类声明 Class Triangle { int bottom; int height; p_shape super; int (*getEdge)(p_triangle triangle); int (*calcArea)(p_triangle triangle); }; //三角形类构造函数 p_triangle Triangle(int bottom, int height); //三角形类析构函数 void _Triangle(void * triangle); //三角形类实例方法 int Triangle_getEdge(p_triangle triangle); //三角形类实例方法,重写父类同名方法 int Triangle_calcArea(p_triangle triangle); #endif /* TRIANGLE_H_ */
Triangle.c
#include "Shape.h" #include "Triangle.h" p_triangle Triangle(int bottom, int height) { p_triangle obj = (p_triangle) malloc(sizeof(Class Triangle)); obj->bottom = bottom; obj->height = height; obj->super = Shape(3); obj->getEdge = Triangle_getEdge; obj->calcArea = Triangle_calcArea; return obj; } void _Triangle(void * triangle) { if (NULL != triangle) { free(triangle); } } int Triangle_getEdge(p_triangle triangle) { return triangle->super->edge; } int Triangle_calcArea(p_triangle triangle) { return triangle->bottom * triangle->height / 2; }
Rectangle.h
#include "Shape.h" #ifndef RECTANGLE_H_ #define RECTANGLE_H_ //矩形类 Class Rectangle; typedef Class Rectangle * p_rectangle; //矩形类声明 Class Rectangle { int bottom; int height; p_shape super; int (*getEdge)(p_rectangle rectangle); int (*calcArea)(p_rectangle rectangle); }; //矩形类构造函数 p_rectangle Rectangle(int bottom, int height); //矩形类析构函数 void _Rectangle(void * rectangle); //矩形类实例方法 int Rectangle_getEdge(p_rectangle rectangle); //矩形类实例方法,重写父类同名方法 int Rectangle_calcArea(p_rectangle rectangle); #endif /* RECTANGLE_H_ */
Rectangle.c
#include "Shape.h" #include "Rectangle.h" p_rectangle Rectangle(int bottom, int height) { p_rectangle obj = (p_rectangle) malloc(sizeof(Class Rectangle)); obj->bottom = bottom; obj->height = height; obj->super = Shape(4); obj->getEdge = Rectangle_getEdge; obj->calcArea = Rectangle_calcArea; return obj; } void _Rectangle(void * rectangle) { if (NULL != rectangle) { free(rectangle); } } int Rectangle_getEdge(p_rectangle rectangle) { return rectangle->super->edge; } int Rectangle_calcArea(p_rectangle rectangle) { return rectangle->bottom * rectangle->height; }
Main.c
#include "Shape.h" #include "Triangle.h" #include "Rectangle.h" #include <stdio.h> int main() { p_triangle triangle = Triangle(4, 5); printf("%d\n", triangle->getEdge(triangle)); printf("%d\n", triangle->calcArea(triangle)); _Triangle(triangle); p_rectangle rectangle = Rectangle(4, 5); printf("%d\n", rectangle->getEdge(rectangle)); printf("%d\n", rectangle->calcArea(rectangle)); _Rectangle(rectangle); return 0; }
编译运行,结果如下:
3 10 4 20
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 字符编码:从基础到乱码解决