C语言实现多态

C语言是一种结构化的编程语言,它本身是面向过程的,但我们可以用一些小技巧来实现面向对象的特性。本篇博文将通过一个实例讲解如何使用C语言实现多态。

定义接口:

//shape.h

#ifndef shape_h
#define shape_h

struct shape;
typedef struct shape shape;

typedef struct shape_virtual_method {
    float (* area) (shape*);
    float (* perimeter) (shape*);
} shape_virtual_method;

struct shape {
    shape_virtual_method* methods;
    char* type;
};

float shape_area(shape* sh);
float shape_perimeter(shape* sh);

#endif // shape_h
//shape.c

#include "shape.h"

float shape_area(shape* sh) {
    return sh->methods->area(sh);
}

float shape_perimeter(shape* sh) {
    return sh->methods->perimeter(sh);
}

定义派生类Circle:

//circle.h

#ifndef circle_h
#define circle_h

#include "shape.h"

typedef struct circle {
    shape base;
    float radius;
} circle;

circle* circle_create(float radius);

#endif // circle_h
//circle.c

#include "circle.h"

static float circle_area(circle* ci) {
    return 3.14f * pow(ci->radius, 2);
}

static float circle_perimeter(circle* ci) {
    return 2 * 3.14 * ci->radius;
}

static shape_virtual_method methods = {
    circle_area,
    circle_perimeter
};

circle* circle_create(float radius) {
    circle *ci = malloc(sizeof(circle));

    ci->base.methods = &methods;
    ci->base.type = "circle";
    ci->radius = radius;

    return ci;
}

定义派生类Rectangle:

//rectangle.h

#ifndef rectangle_h
#define rectangle_h

#include "shape.h"

typedef struct rectangle {
    shape base;
    float width;
    float height;
} rectangle;

rectangle* rectangle_create(float width, float height);

#endif // rectangle_h
//rectangle.c

#include "rectangle.h"

static float rectangle_area(rectangle* re) {
    return re->width * re->height;
}

static float rectangle_perimeter(rectangle* re) {
    return 2 * (re->width + re->height);
}

static shape_virtual_method methods = {
    rectangle_area,
    rectangle_perimeter
};

rectangle* rectangle_create(float width, float height) {
    rectangle *re = malloc(sizeof(rectangle));

    re->base.methods = &methods;
    re->base.type = "rectangle";
    re->width = width;
    re->height = height;

    return re;
}

测试:

//main.c

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#include "shape.h"
#include "circle.h"
#include "rectangle.h"

int main()
{
    shape* shapes[4];
    shapes[0] = circle_create(3);
    shapes[1] = circle_create(0.5);
    shapes[2] = rectangle_create(2, 3);
    shapes[3] = rectangle_create(3, 4);

    int i;
    for(i = 0; i < sizeof(shapes) / sizeof(shape*); i++) {
        printf("%d> type: %s area: %f, perimeter: %f \n",
               i, shapes[i]->type, shape_area(shapes[i]), shape_perimeter(shapes[i]));
    }

    return 0;
}

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。