zoukankan      html  css  js  c++  java
  • C语言实现OOP 版本3 :简化代码

    我倒是不追求代码和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 
    ", _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 > 0);
    
        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 
    ", 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 > 0 && y > 0);
    
        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=0;
        for(i=0;i<length;i++)
        {
            Shape *shape = shapes[i];
            shape->area(shape);
            shape->release(shape);
        }
    }
    
    int main()
    {
        Shape *p[3] = {makeCircle(3.2),makeCircle(3.2),makeRectangle(3,4)};
        printShapeArea(p,3);
        return 0;
    }
  • 相关阅读:
    发现个atan2的正确使用方式
    Forward+ Shading架构
    fatal: unable to connect to gitee.com: gitee.com[0: 180.97.125.228]: errno=Unknown error 解决方案
    HDFS HA(高可用性)集群规划
    如何使用RTP引擎对语音编码进行转码
    关于 Angular 应用 tsconfig.json 中的 target 属性
    浅谈 Orbeon form builder 的权限控制
    关于 Angular 应用 tsconfig.json 中的 lib 属性
    orbeon form 通过 url 的方式同第三方应用集成的开发明细
    orbeon form 的配置介绍
  • 原文地址:https://www.cnblogs.com/code-style/p/3219415.html
Copyright © 2011-2022 走看看