日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

c语言模拟java面向对象_纯c语言实现面向对象分析与示例分享

發布時間:2023/12/9 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c语言模拟java面向对象_纯c语言实现面向对象分析与示例分享 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

#include

#include

//接口

#ifndef Interface

#define Interface struct

#endif

//類

#ifndef Class

#define Class struct

#endif

//抽象形狀類

Class Shape;

typedef Class Shape shape;

//抽象形狀類的方法聲明

shape* Shape(int edges);

int shape_getEdges(shape *);

int shape_getArea(void);

void _Shape(shape *);

//三角形類

Class Triangle;

typedef Class Triangle triangle;

//三角形類的方法聲明

triangle * Triangle(int bottom, int height);

int triangle_getEdges(triangle *);

int triangle_getArea(triangle *);

void _Triangle(triangle *);

//矩形類

Class Rectangle;

typedef Class Rectangle rectangle;

//矩形類的方法聲明

rectangle * Rectangle(int bottom, int height);

int rectangle_getEdges(rectangle *);

int rectangle_getArea(rectangle *);

void _Rectangle(rectangle *);

//抽象形狀類實現

Class Shape

{

int edges;

int (*getEdges)(shape*);

int (*getArea)(void);

};

//形狀類構造函數

shape* Shape(int edges)

{

shape * obj = (shape *) malloc(sizeof(shape));

obj->edges = edges;

obj->getEdges = shape_getEdges;

obj->getArea = shape_getArea;

return obj;

}

int shape_getEdges(shape* obj)

{

return obj->edges;

}

int shape_getArea(void)

{

return -1;

}

//形狀類析構函數

void _Shape(shape * obj)

{

if(obj == NULL)

return;

free(obj);

}

//三角形類實現

Class Triangle

{

shape * super;

int bottom;

int height;

int (*getEdges)(triangle *);

int (*getArea)(triangle *);

};

//三角形類構造函數

triangle * Triangle(int bottom, int height)

{

triangle* obj = (triangle*) malloc(sizeof(triangle));

//調用Shape構造函數用于實現繼承

obj->super = Shape(3);

obj->bottom = bottom;

obj->height = height;

obj->getEdges = triangle_getEdges;

obj->getArea = triangle_getArea;

return obj;

}

int triangle_getEdges(triangle * obj)

{

return obj->super->edges;

}

int triangle_getArea(triangle * obj)

{

return (obj->bottom * obj->height) / 2;

}

//三角形類析構函數

void _Triangle(triangle * triangle)

{

_Shape(triangle->super);

if(triangle == NULL)

{

return;

}

free(triangle);

}

//矩形類實現

Class Rectangle

{

shape * super;

int bottom;

int height;

int (*getEdges)(rectangle *);

int (*getArea)(rectangle *);

};

//矩形類構造函數

rectangle * Rectangle(int bottom, int height)

{

rectangle * obj = (rectangle *)malloc(sizeof(rectangle));

//調用Shape構造函數用于實現繼承

obj->super = Shape(4);

obj->bottom = bottom;

obj->height = height;

obj->getEdges = rectangle_getEdges;

obj->getArea = rectangle_getArea;

return obj;

}

int rectangle_getEdges(rectangle * obj)

{

return obj->super->edges;

}

int rectangle_getArea(rectangle * obj)

{

return (obj->bottom * obj->height);

}

//矩形類析構函數

void _Rectangle(rectangle * obj)

{

_Shape(obj->super);

if(obj == NULL)

{

return;

}

free(obj);

}

//測試

void main(){

shape* shapeObj = Shape(0);

printf("%d\n", shapeObj->getEdges(shapeObj));

printf("%d\n", shapeObj->getArea());

_Shape(shapeObj);

triangle* triangleObj = Triangle(4, 5);

printf("%d\n", triangleObj->getEdges(triangleObj));

printf("%d\n", triangleObj->getArea(triangleObj));

_Triangle(triangleObj);

rectangle* rectangleObj = Rectangle(4, 5);

printf("%d\n", rectangleObj->getEdges(rectangleObj));

printf("%d\n", rectangleObj->getArea(rectangleObj));

_Rectangle(rectangleObj);

}

總結

以上是生活随笔為你收集整理的c语言模拟java面向对象_纯c语言实现面向对象分析与示例分享的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。