当前位置: 首页 > news >正文

广州网站建设商家seo排名平台

广州网站建设商家,seo排名平台,泉州做网站seo的,构建平台还是搭建平台在 C 语言中,虽然没有内建的面向对象编程(OOP)特性(如封装、继承、多态),但通过一些编程技巧,我们仍然可以模拟实现这些概念。下面将用通俗易懂的方式,逐步介绍如何在 C 中实现封装、…

在 C 语言中,虽然没有内建的面向对象编程(OOP)特性(如封装、继承、多态),但通过一些编程技巧,我们仍然可以模拟实现这些概念。下面将用通俗易懂的方式,逐步介绍如何在 C 中实现封装、继承和多态。

1. 封装(Encapsulation)

封装是指将数据和操作数据的函数绑定在一起,隐藏内部实现细节,只暴露必要的接口。在 C 中,我们可以通过 struct 和相关的函数来实现封装。

假设我们要创建一个“矩形”对象,包含宽度和高度,并提供计算面积的功能。

步骤:

  1. 定义结构体(隐藏内部细节)
  2. 提供创建和操作该结构体的函数

实现:

// Rectangle.h
#ifndef RECTANGLE_H
#define RECTANGLE_Htypedef struct Rectangle Rectangle;// 创建矩形
Rectangle* Rectangle_create(double width, double height);// 销毁矩形
void Rectangle_destroy(Rectangle* rect);// 计算面积
double Rectangle_getArea(const Rectangle* rect);#endif // RECTANGLE_H// Rectangle.c
#include <stdlib.h>
#include "Rectangle.h"// 定义结构体(隐藏在 .c 文件中)
struct Rectangle {double width;double height;
};// 创建矩形
Rectangle* Rectangle_create(double width, double height) {Rectangle* rect = (Rectangle*)malloc(sizeof(Rectangle));if (rect != NULL) {rect->width = width;rect->height = height;}return rect;
}// 销毁矩形
void Rectangle_destroy(Rectangle* rect) {free(rect);
}// 计算面积
double Rectangle_getArea(const Rectangle* rect) {if (rect == NULL) return 0.0;return rect->width * rect->height;
}// main.c
#include <stdio.h>
#include "Rectangle.h"int main() {Rectangle* rect = Rectangle_create(5.0, 3.0);if (rect != NULL) {printf("面积: %.2f\n", Rectangle_getArea(rect));Rectangle_destroy(rect);}return 0;
}

说明:

  • 隐藏实现Rectangle 的具体结构体定义在 Rectangle.c 中,外部无法直接访问其成员变量。
  • 接口函数:通过 Rectangle_createRectangle_destroyRectangle_getArea 提供对 Rectangle 对象的操作。

2. 继承(Inheritance)

继承允许一个“子类”拥有“父类”的属性和行为。在 C 中,我们可以通过在子结构体中包含父结构体来模拟继承。

基类“形状”和子类“矩形”和“圆形”

步骤:

  1. 定义基类结构体,包含一个指向函数的指针(模拟虚函数表)。
  2. 定义子类结构体,在其中包含基类结构体。
  3. 实现子类的功能

实现:

// Shape.h
#ifndef SHAPE_H
#define SHAPE_Htypedef struct Shape Shape;// 虚函数表
typedef struct {double (*getArea)(const Shape* self);void (*destroy)(Shape* self);
} ShapeVTable;// 基类结构体
struct Shape {ShapeVTable* vtable;
};// 基类接口函数
double Shape_getArea(const Shape* self);
void Shape_destroy(Shape* self);#endif // SHAPE_H// Shape.c
#include "Shape.h"double Shape_getArea(const Shape* self) {if (self && self->vtable && self->vtable->getArea) {return self->vtable->getArea(self);}return 0.0;
}void Shape_destroy(Shape* self) {if (self && self->vtable && self->vtable->destroy) {self->vtable->destroy(self);}
}// Rectangle.h
#ifndef RECTANGLE_H
#define RECTANGLE_H#include "Shape.h"typedef struct {Shape base; // 继承自 Shapedouble width;double height;
} Rectangle;// 创建矩形
Shape* Rectangle_create(double width, double height);#endif // RECTANGLE_H// Rectangle.c
#include <stdlib.h>
#include "Rectangle.h"// 矩形的虚函数实现
double Rectangle_getArea(const Shape* self) {const Rectangle* rect = (const Rectangle*)self;return rect->width * rect->height;
}void Rectangle_destroy_impl(Shape* self) {free(self);
}// 定义矩形的虚函数表
ShapeVTable rectangle_vtable = {.getArea = Rectangle_getArea,.destroy = Rectangle_destroy_impl
};// 创建矩形
Shape* Rectangle_create(double width, double height) {Rectangle* rect = (Rectangle*)malloc(sizeof(Rectangle));if (rect != NULL) {rect->base.vtable = &rectangle_vtable;rect->width = width;rect->height = height;}return (Shape*)rect;
}// Circle.h
#ifndef CIRCLE_H
#define CIRCLE_H#include "Shape.h"typedef struct {Shape base; // 继承自 Shapedouble radius;
} Circle;// 创建圆形
Shape* Circle_create(double radius);#endif // CIRCLE_H// Circle.c
#include <stdlib.h>
#include "Circle.h"
#include <math.h>// 圆形的虚函数实现
double Circle_getArea(const Shape* self) {const Circle* circle = (const Circle*)self;return M_PI * circle->radius * circle->radius;
}void Circle_destroy_impl(Shape* self) {free(self);
}// 定义圆形的虚函数表
ShapeVTable circle_vtable = {.getArea = Circle_getArea,.destroy = Circle_destroy_impl
};// 创建圆形
Shape* Circle_create(double radius) {Circle* circle = (Circle*)malloc(sizeof(Circle));if (circle != NULL) {circle->base.vtable = &circle_vtable;circle->radius = radius;}return (Shape*)circle;
}// main.c
#include <stdio.h>
#include "Shape.h"
#include "Rectangle.h"
#include "Circle.h"int main() {Shape* shapes[2];shapes[0] = Rectangle_create(5.0, 3.0); // 创建矩形shapes[1] = Circle_create(2.0);         // 创建圆形for (int i = 0; i < 2; ++i) {printf("图形 %d 的面积: %.2f\n", i + 1, Shape_getArea(shapes[i]));Shape_destroy(shapes[i]);}return 0;
}

说明:

  • 基类 Shape:包含一个虚函数表 vtable,用于指向具体实现的函数。
  • 子类 RectangleCircle
    • 包含 Shape 作为第一个成员,实现“继承”。
    • 定义自己的虚函数(如 getAreadestroy_impl)。
    • 分别创建自己的虚函数表,并在创建时将 vtable 指向自己的表。
  • 多态:在 main 中,通过基类指针 Shape* 调用 getArea,根据实际对象类型(矩形或圆形)执行不同的函数。

3. 多态(Polymorphism)

多态允许不同类型的对象通过相同的接口调用不同的实现。在上面的继承示例中,我们已经部分实现了多态。下面进一步解释多态的实现。

Shape 基类中定义了虚函数表 ShapeVTable,包含 getAreadestroy 函数指针。每个子类(如 RectangleCircle)都提供了自己的实现,并在创建时将 vtable 指向自己的函数表。

如何工作:

  1. 统一接口:所有形状都通过 Shape* 指针进行操作。
  2. 具体实现:不同的形状(矩形、圆形)有各自的 getArea 实现。
  3. 调用时自动选择:根据对象的实际类型,调用相应的 getArea 函数。

示例解释:

for (int i = 0; i < 2; ++i) {printf("图形 %d 的面积: %.2f\n", i + 1, Shape_getArea(shapes[i]));Shape_destroy(shapes[i]);
}
  • Shape_getArea(shapes[i]) 会根据 shapes[i]vtable 指向不同的 getArea 实现,自动计算出矩形或圆形的面积。
http://www.hkea.cn/news/437617/

相关文章:

  • 广西南宁网络营销网站网站权重优化
  • 黄山网站设计公司营销网站建设多少钱
  • 网站建设招标评分表湖南关键词优化推荐
  • 淘宝上成都网站建设如何制作视频网站
  • 最吃香的男生十大手艺5g网络优化
  • 河源哪里做网站网络项目怎么推广
  • 网站闭关保护怎么做广州百度seo 网站推广
  • 可以在线做动图的网站近期重大新闻事件
  • 伊犁州建设局网站怎么做微信小程序
  • 做网站需要买主机那新媒体营销方式有几种
  • 网络推广seo公司seo排名的方法
  • 南山做网站多少钱百度资讯
  • 西安哪里有做网站的小学生收集的新闻10条
  • 做游戏网站有几个要素seo网站关键词优化报价
  • 蓬业东莞网站建设技术支持东莞做网站公司首选
  • 网站版式设计获客渠道有哪些
  • 今日军事新闻简短扬州seo优化
  • 国外好看的教育类网站模板下载东莞做网站最好的是哪家
  • 微擎与wordpress快速优化seo软件推广方法
  • 英文网站设计哪家好免费网站搭建
  • 网站建设公司 销量深圳谷歌seo公司
  • 新蔡哪有做网站建设的全球疫情今天最新消息
  • 怎么做平台网站百度seo报价方法
  • 帮人做网站 怎么收费怎么用网络推广
  • 网站排名优化建设百度广告投放技巧
  • 文件服务器网站搭建教程好的竞价托管公司
  • 黑龙江省城乡和住房建设厅网站首页百度链接地址
  • 网站模板修改工具专业seo关键词优化
  • 口碑好的句容网站建设yahoo搜索
  • 深圳网站建设外贸公司价格网络营销的背景和意义