我倒是不追求代码和C++相似,但是应该追求简洁的代码,下面是一个新的尝试


shape.h

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

shape.c

#include <stdlib.h>
#include "shape.h" void release(void *shape)
{
free(((Shape*)shape)->shapeData);
free(shape);
}

circle.h

#ifndef CIRCLE_H
#define CIRCLE_H #include "shape.h" typedef struct
{
double r;
}Circle; Shape* makeCircle(double r); #endif

circle.c

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

rectange.h

#ifndef RECTANGLE_H
#define RECTANGLE_H typedef struct{
float x;
float y;
}Rectangle; Shape *makeRectangle(float x, float y);
#endif

rectange.c

#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include "shape.h"
#include "rectangle.h" static void area(void *shape)
{
Rectangle *rectangle = (Rectangle*)((Shape *)shape)->shapeData;
printf("the rectangle area is %f \n", rectangle->x * rectangle->y);
} Shape* makeRectangle(float x, float y)
{
Shape *shape = (Shape *)malloc(sizeof(Shape));
Rectangle *rectangle = (Rectangle *)malloc(sizeof(Rectangle));
assert(shape != NULL && rectangle != NULL);
assert(x > && y > ); rectangle->x = x;
rectangle->y = y;
shape->shapeData = rectangle;
shape->area = &area;
shape->release = &release; return shape;
}

main.c

#include <stdio.h>
#include "shape.h"
#include "circle.h"
#include "rectangle.h" void printShapeArea(Shape **shapes,int length)
{
int i=;
for(i=;i<length;i++)
{
Shape *shape = shapes[i];
shape->area(shape);
shape->release(shape);
}
} int main()
{
Shape *p[] = {makeCircle(3.2),makeCircle(3.2),makeRectangle(,)};
printShapeArea(p,);
return ;
}

C语言实现OOP 版本3 :简化代码的更多相关文章

  1. C语言实现OOP 版本2

    写版本2的原因,还是发现在不同的具体图形模块里发现了重复的release代码,这是坏味道,所以还是决定消除这些重复代码,DRY! shape.h #ifndef SHAPE_H #define SHA ...

  2. 一个UUID生成算法的C语言实现 --- WIN32版本 .

    一个UUID生成算法的C语言实现——WIN32版本   cheungmine 2007-9-16   根据定义,UUID(Universally Unique IDentifier,也称GUID)在时 ...

  3. C# 语言规范_版本5.0 (第2章 词法结构)

    1. 词法结构 1.1 程序 C# 程序 (program) 由一个或多个源文件 (source file) 组成,源文件的正式名称是编译单元 (compilation unit)(第 9.1 节). ...

  4. 几种不同程序语言的HMM版本

    几种不同程序语言的HMM版本 “纸上得来终觉浅,绝知此事要躬行”,在继续翻译<HMM学习最佳范例>之前,这里先补充几个不同程序语言实现的HMM版本,主要参考了维基百科.读者有兴趣的话可以研 ...

  5. 链表中用标兵结点简化代码 分类: c/c++ 2014-09-29 23:10 475人阅读 评论(0) 收藏

    标兵结点(头结点)是在链表中的第一个结点,不存放数据,仅仅是个标记 利用标兵结点可以简化代码.下面实现双向链表中的按值删除元素的函数,分别实现 带标兵结点和不带标兵结点两版本,对比可见标兵结点的好处. ...

  6. 用block做事件回调来简化代码,提高开发效率

       我们在自定义view的时候,通常要考虑view的封装复用,所以如何把view的事件回调给Controller就是个需要好好考虑的问题, 一般来说,可选的方式主要有target-action和de ...

  7. LevelDB源码分析--使用Iterator简化代码设计

    我们先来参考来至使用Iterator简化代码2-TwoLevelIterator的例子,略微修改希望能帮助更加容易立即,如果有不理解请各位看客阅读原文. 下面我们再来看一个例子,我们为一个书店写程序, ...

  8. C#泛型简化代码量示例

    泛型简化代码量 下是我在项目中通过泛型来简化工作的一个Demo,记录一下: using System; using System.Collections.Generic;   namespace My ...

  9. 玩转UITableView系列(一)--- 解耦封装、简化代码、适者生存!

    UITableView这个iOS开发中永远绕不开的UIView,那么就不可避免的要在多个页面多种场景下反复摩擦UITableView,就算是刚跳进火坑不久的iOS Developer也知道实现UITa ...

随机推荐

  1. android开发论坛

    http://www.hiapk.com/ http://bbs.hiapk.com/ http://bbs.gfan.com/ http://bbs.anzhi.com/ http://www.ap ...

  2. 黑马程序员_Java_String

    String类 一.概述 字符串是一个特殊的对象. 字符串一旦初始化就不可以被改变. String s1 = "abc";//s1是一个类类型变量,"abc"是 ...

  3. Jtree(节点的渲染+资源管理器)(2)

    上一次我们建立一个比较简单的资源管理器,这次我们说一下上面的资源管理器的问题,并且得尽量的贴近windows的资源管理器. 这样一个简单的资源管理树就完成了,下面我们说说它的问题: ① 图片和外观和W ...

  4. cf437D The Child and Zoo

    D. The Child and Zoo time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  5. HTML5的local storage存储的数据到底存到哪去了

    原文地址:http://zhidao.baidu.com/link?url=m6p5MLv0R46lDCd_Vnrry4XOMbdCwgV5fzs3tj5Jeyht1nPkAZ9OrO23njYBY1 ...

  6. poj 3273 Monthly Expense(二分搜索之最大化最小值)

    Description Farmer John ≤ moneyi ≤ ,) that he will need to spend each day over the next N ( ≤ N ≤ ,) ...

  7. MapReduce新版客户端API源码分析

    使用MapReduce新版客户端API提交MapReduce Job需要使用 org.apache.hadoop.mapreduce.Job 类.JavaDoc给出以下使用范例. // Create ...

  8. JAVA学习第三十课(经常使用对象API)- String类:类方法练习

    intern方法 public class Main { public static void main(String[] args) { String str1 = new String(" ...

  9. 慕课linux学习笔记(一)centOS的安装

    在VMware8上安装centos6.3 准备的文件 新建虚拟机 选择新建一个空的虚拟机 选择linux和centos 分配20G的硬盘空间 ' 修改配置 调整内存空间 桥接:虚拟机和真实机通讯使用的 ...

  10. iOS中AutoLayer自动布局流程及相关方法【转】

    转自:http://my.oschina.net/w11h22j33/blog/208574 关于UIView的Layer,IOS提供了三个方法: 1.layoutSubviews 在iOS5.1和之 ...