码迷,mamicode.com
首页 > 编程语言 > 详细

C语言实现多态

时间:2015-05-24 17:15:33      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:

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;
}

 

C语言实现多态

标签:

原文地址:http://www.cnblogs.com/ksco/p/4526056.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!