[开发工具] C语言面向对象的简单例子

[复制链接]
 楼主| OKAKAKO 发表于 2024-4-24 16:00 | 显示全部楼层 |阅读模式

C语言是一种面向过程的语言,但是也可以用结构体和函数指针来模拟面向对象的特性,比如封装、继承和多态。下面我们来看一些具体的例子和应用。

封装是指把对象的属性和方法封装在一起,提供一个接口给外部调用,隐藏内部细节。在C语言中,我们可以用结构体来定义对象的属性,用函数指针来定义对象的方法,然后把它们放在一个结构体中,形成一个类。例如,我们可以定义一个人类:

  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. // 定义人类
  4. struct person {
  5.     // 属性
  6.     char *name;
  7.     int age;
  8.     // 方法
  9.     void (*say_hello)(struct person *p);
  10. };

  11. // 定义人类的方法
  12. void say_hello(struct person *p) {
  13.     printf("Hello, I am %s, %d years old.\n", p->name, p->age);
  14. }

  15. // 创建人类的实例
  16. struct person *create_person(char *name, int age) {
  17.     struct person *p = malloc(sizeof(struct person));
  18.     p->name = name;
  19.     p->age = age;
  20.     p->say_hello = say_hello;
  21.     return p;
  22. }

  23. // 使用人类的实例
  24. int main() {
  25.     struct person *p1 = create_person("Alice", 20);
  26.     struct person *p2 = create_person("Bob", 25);
  27.     p1->say_hello(p1);
  28.     p2->say_hello(p2);
  29.     free(p1);
  30.     free(p2);
  31.     return 0;
  32. }

继承是指子类可以复用父类的属性和方法,同时可以添加或覆盖父类的属性和方法。在C语言中,我们可以用结构体嵌套来实现继承,即把父类作为子类的第一个成员。例如,我们可以定义一个学生类,继承自人类:

  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. // 定义人类
  4. struct person {
  5.     // 属性
  6.     char *name;
  7.     int age;
  8.     // 方法
  9.     void (*say_hello)(struct person *p);
  10. };

  11. // 定义人类的方法
  12. void say_hello(struct person *p) {
  13.     printf("Hello, I am %s, %d years old.\n", p->name, p->age);
  14. }

  15. // 创建人类的实例
  16. struct person *create_person(char *name, int age) {
  17.     struct person *p = malloc(sizeof(struct person));
  18.     p->name = name;
  19.     p->age = age;
  20.     p->say_hello = say_hello;
  21.     return p;
  22. }


  23. // 定义学生类
  24. struct student {
  25.     // 继承自人类
  26.     struct person base;
  27.     // 属性
  28.     char *school;
  29.     // 方法
  30.     void (*study)(struct student *s);
  31. };

  32. // 定义学生类的方法
  33. void study(struct student *s) {
  34.     printf("%s is studying at %s.\n", s->base.name, s->school);
  35. }

  36. // 创建学生类的实例
  37. struct student *create_student(char *name, int age, char *school) {
  38.     struct student *s = malloc(sizeof(struct student));
  39.     s->base.name = name;
  40.     s->base.age = age;
  41.     s->base.say_hello = say_hello; // 复用父类的方法
  42.     s->school = school;
  43.     s->study = study;
  44.     return s;
  45. }

  46. // 使用学生类的实例
  47. int main() {
  48.     struct student *s1 = create_student("Charlie", 18, "MIT");
  49.     struct student *s2 = create_student("David", 19, "Stanford");
  50.     s1->base.say_hello(&s1->base); // 调用父类的方法
  51.     s2->base.say_hello(&s2->base);
  52.     s1->study(s1); // 调用子类的方法
  53.     s2->study(s2);
  54.     free(s1);
  55.     free(s2);
  56.     return 0;
  57. }

多态是指不同类型的对象可以使用相同的接口,根据对象的具体类型执行不同的行为。在C语言中,我们可以用函数指针来实现多态,即把不同类型的对象都转换为一个通用类型,然后调用它们共有的函数指针。例如,我们可以定义一个动物类和两个子类:狗类和猫类,分别实现动物类的叫声方法:

  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. // 定义动物类
  4. struct animal {
  5.     // 属性
  6.     char *name;
  7.     // 方法
  8.     void (*make_sound)(struct animal *a);
  9. };

  10. // 定义狗类
  11. struct dog {
  12.     // 继承自动物类
  13.     struct animal base;
  14.     // 属性
  15.     char *breed;
  16. };

  17. // 定义猫类
  18. struct cat {
  19.     // 继承自动物类
  20.     struct animal base;
  21.     // 属性
  22.     char *color;
  23. };

  24. // 定义动物类的方法
  25. void make_sound(struct animal *a) {
  26.     printf("%s is making sound.\n", a->name);
  27. }

  28. // 定义狗类的方法
  29. void dog_make_sound(struct dog *d) {
  30.     printf("%s is barking.\n", d->base.name);
  31. }

  32. // 定义猫类的方法
  33. void cat_make_sound(struct cat *c) {
  34.     printf("%s is meowing.\n", c->base.name);
  35. }

  36. // 创建动物类的实例
  37. struct animal *create_animal(char *name) {
  38.     struct animal *a = malloc(sizeof(struct animal));
  39.     a->name = name;
  40.     a->make_sound = make_sound;
  41.     return a;
  42. }

  43. // 创建狗类的实例
  44. struct dog *create_dog(char *name, char *breed) {
  45.     struct dog *d = malloc(sizeof(struct dog));
  46.     d->base.name = name;
  47.     d->base.make_sound = (void (*)(struct animal *))dog_make_sound; // 覆盖父类的方法
  48.     d->breed = breed;
  49.     return d;
  50. }

  51. // 创建猫类的实例
  52. struct cat *create_cat(char *name, char *color) {
  53.     struct cat *c = malloc(sizeof(struct cat));
  54.     c->base.name = name;
  55.     c->base.make_sound = (void (*)(struct animal *))cat_make_sound; // 覆盖父类的方法
  56.     c->color = color;
  57.     return c;
  58. }

  59. // 使用动物类的实例
  60. int main() {
  61.     struct animal *a1 = create_animal("Tom");
  62.     struct dog *d1 = create_dog("Spike", "Bulldog");
  63.     struct cat *c1 = create_cat("Jerry", "Brown");
  64.    
  65.     // 多态:不同类型的对象使用相同的接口,执行不同的行为
  66.     a1->make_sound(a1); // 调用动物类的方法
  67.     d1->base.make_sound(&d1->base); // 调用狗类的方法
  68.     c1->base.make_sound(&c1->base); // 调用猫类的方法
  69.    
  70.     free(a1);
  71.     free(d1);
  72.     free(c1);
  73.    
  74.     return 0;
  75. }

以上就是C语言面向对象的一些例子。
digit0 发表于 2024-4-24 16:16 | 显示全部楼层
面向对象的方法确实不错!可以参考用!
LEDyyds 发表于 2024-4-25 14:05 | 显示全部楼层
C语言面向对象挺有意思
中国龙芯CDX 发表于 2024-4-28 17:29 | 显示全部楼层
C语言面向对象的简单例子确实很有想法,C还是很强大的
米多0036 发表于 2024-4-28 21:21 来自手机 | 显示全部楼层
C语言是很全能的呢,面向对象的例子也很有趣。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

257

主题

2006

帖子

4

粉丝
快速回复 在线客服 返回列表 返回顶部