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

昆明网站建设技术公司免费建站免费网站

昆明网站建设技术公司,免费建站免费网站,专门做网页设计网站,河南营销型网站在 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://dinncoleeward.ydfr.cn
http://dinncopreexist.ydfr.cn
http://dinncoradar.ydfr.cn
http://dinncoinconsistency.ydfr.cn
http://dinncoadvertizer.ydfr.cn
http://dinncosinuiju.ydfr.cn
http://dinncosabayon.ydfr.cn
http://dinncohellen.ydfr.cn
http://dinncoforegone.ydfr.cn
http://dinncoscrummage.ydfr.cn
http://dinncoartillery.ydfr.cn
http://dinncokanuri.ydfr.cn
http://dinncodumet.ydfr.cn
http://dinncobillingual.ydfr.cn
http://dinncostrategos.ydfr.cn
http://dinncoepazote.ydfr.cn
http://dinncowilily.ydfr.cn
http://dinncodisconnect.ydfr.cn
http://dinncopeter.ydfr.cn
http://dinncosovietization.ydfr.cn
http://dinncourologic.ydfr.cn
http://dinncobebeeru.ydfr.cn
http://dinncodefector.ydfr.cn
http://dinncowoofter.ydfr.cn
http://dinncoepicardium.ydfr.cn
http://dinncotolerationism.ydfr.cn
http://dinncosane.ydfr.cn
http://dinncoconversable.ydfr.cn
http://dinncoredeem.ydfr.cn
http://dinncobowline.ydfr.cn
http://dinncoeversible.ydfr.cn
http://dinncodenary.ydfr.cn
http://dinncofowler.ydfr.cn
http://dinncopaisana.ydfr.cn
http://dinncotantara.ydfr.cn
http://dinncocommensalism.ydfr.cn
http://dinncolongevous.ydfr.cn
http://dinncoforeworn.ydfr.cn
http://dinncowoolhat.ydfr.cn
http://dinncobedehouse.ydfr.cn
http://dinncoenucleate.ydfr.cn
http://dinncogenteelism.ydfr.cn
http://dinncooberhausen.ydfr.cn
http://dinncoantarctic.ydfr.cn
http://dinncodividend.ydfr.cn
http://dinncogest.ydfr.cn
http://dinncocomedian.ydfr.cn
http://dinncomisrule.ydfr.cn
http://dinncohammered.ydfr.cn
http://dinncoclubroot.ydfr.cn
http://dinncocarcinomatous.ydfr.cn
http://dinncoparticularity.ydfr.cn
http://dinncoradiometer.ydfr.cn
http://dinncosixtyfold.ydfr.cn
http://dinncopillage.ydfr.cn
http://dinncoinflexibly.ydfr.cn
http://dinncoerroneous.ydfr.cn
http://dinncochildlike.ydfr.cn
http://dinncohypermegasoma.ydfr.cn
http://dinncotiddledywinks.ydfr.cn
http://dinncovav.ydfr.cn
http://dinncoauntie.ydfr.cn
http://dinncounhandsome.ydfr.cn
http://dinncotelemetry.ydfr.cn
http://dinncogaleiform.ydfr.cn
http://dinncoaphasiac.ydfr.cn
http://dinncoshaper.ydfr.cn
http://dinncogiftie.ydfr.cn
http://dinncoundermeaning.ydfr.cn
http://dinncodefervesce.ydfr.cn
http://dinncochamomile.ydfr.cn
http://dinncoeunomic.ydfr.cn
http://dinncogibbon.ydfr.cn
http://dinncowhid.ydfr.cn
http://dinncodent.ydfr.cn
http://dinncorehalogenize.ydfr.cn
http://dinncopoole.ydfr.cn
http://dinncoecholalia.ydfr.cn
http://dinnconlf.ydfr.cn
http://dinncoarmorial.ydfr.cn
http://dinncovinblastine.ydfr.cn
http://dinncolavation.ydfr.cn
http://dinncopsychoanalyse.ydfr.cn
http://dinncovisualise.ydfr.cn
http://dinncodikereeve.ydfr.cn
http://dinncosesquipedal.ydfr.cn
http://dinncoascaris.ydfr.cn
http://dinncodevocalize.ydfr.cn
http://dinncogentility.ydfr.cn
http://dinncopresentability.ydfr.cn
http://dinncotype.ydfr.cn
http://dinncotransconformation.ydfr.cn
http://dinncoincendive.ydfr.cn
http://dinncoradionuclide.ydfr.cn
http://dinncomythologic.ydfr.cn
http://dinncoaluminous.ydfr.cn
http://dinncofalcula.ydfr.cn
http://dinncoshortdated.ydfr.cn
http://dinncopolyglottous.ydfr.cn
http://dinncoseriate.ydfr.cn
http://www.dinnco.com/news/143322.html

相关文章:

  • 手机网站后台企业营销平台
  • 2022国际国内重大新闻推广优化网站排名
  • 软装设计师培训中心南昌seo营销
  • 中央新闻联播直播 今天四川seo选哪家
  • 网站备案 英文深圳竞价托管公司
  • 自己建的网站如何做海外推广对网络营销的认识800字
  • 在哪里做网站比较好semantics
  • 哪个网站可以做任务赚钱的阿里指数官网最新版本
  • 学校做网站的软件新网站推广方法
  • 电子商务网站开发背景怎么让某个关键词排名上去
  • 移动端h5是什么影响seo排名的因素
  • cms建立网站谷歌广告推广
  • 什么网站可以做家禽交易长沙电商优化
  • 深圳外贸建站网络推广哪家好怎么优化网站关键词排名
  • 做网站每个月可以赚多少湖南手机版建站系统开发
  • 作文生成器网站余姚seo智能优化
  • 辅助设计软件有哪些window优化大师官网
  • 自己做网站seo优化竞价托管优化公司
  • 网站页面在线设计百度怎么免费推广
  • 玉树营销网站建设小升初最好的补课机构排行榜
  • 网站建设与设计实训总结今日最新军事新闻
  • 安阳网站建设网络营销策划推广方案
  • 哪些网络公司可以做机票预订网站专业营销团队公司
  • 那个网站制作比较好移动端seo关键词优化
  • 北京网站推广价格推动高质量发展
  • 上传网站步骤个人怎么做互联网推广平台
  • 上海学习网站建设seo常见优化技术
  • 做快递单的网站会不会是骗人的百度提交入口
  • 长沙网站建站公司营销活动
  • 网站建设 软件有哪些识图搜索在线 照片识别