创建如下文件目录 :
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 ", triangle->getEdge(triangle)); printf("%d ", triangle->calcArea(triangle)); _Triangle(triangle); p_rectangle rectangle = Rectangle(4, 5); printf("%d ", rectangle->getEdge(rectangle)); printf("%d ", rectangle->calcArea(rectangle)); _Rectangle(rectangle); return 0; }
编译运行,结果如下:
3 10 4 20