美文网首页
C语言实现面向对象

C语言实现面向对象

作者: 草莓2020 | 来源:发表于2020-12-22 20:59 被阅读0次
person为父类,child为子类
代码实现如下
person.h
typedef struct Person {
// 模拟类的成员属性
char *name;
int age;
// 模拟类的方法
void (*showInfo)(Person_T);
int (*compare)(void *,void *,int (*compare)(void *,void *));
}*Person_T;

// 模拟对象的创建
Person_T Person_new(char *name,int age);
void Person_free(Person_T person);
person.c
#include"person.h"
#include<stdio.h>
#include<stdlib.h>

// 打印属性信息
void Person_info(Person_T person){
printf("name:%s age:%d\n",person->name,person->age);
}

// 信息比较函数
int Person_compare(void *a,void *b,int (*compare)(void *,void *)){
return (*compare)(a,b);
}

// 创建对象并初始化
Person_T Person_new(char *name,int age){
Person_T person = malloc(sizeof(struct Person));
// 初始化成员属性
person->name = name;
person->age = age;
// 初始化方法
person->showInfo = Person_info;
person->compare = Person_compare;
return person;
}

// 释放指针对内存的引用
void Person_free(Person_T person){
free(person);
}
child.h
#include"person.h"

typedef struct Child {
// 模拟类的成员属性
Person_T person;
float hight;
}*Child_T;

// 模拟对象的创建
Child_T Child_new(char *name,int age,float hight);
void Child_free(Child_T child);
child.c
#include"child.h"
#include<stdio.h>
#include<stdlib.h>

// 打印属性信息
void Child_info(Child_T child){
printf("name:%s age:%d height:%.2f\n",child->person->name,child->person->age,child->hight);
}

// 创建对象并初始化
Child_T Child_new(char *name,int age,float hight){
Child_T child = malloc(sizeof(struct Child));
// 初始化成员属性
child->person = Person_new(name,age);
child->hight = hight;
// 重写继承父类的showInfo方法
child->person->showInfo = Child_info;
return child;
}

// 释放指针对内存的引用
void Child_free(Child_T child){
Person_free(child->person);
free(child);
}
test.c
#include<stdio.h>
#include<string.h>
#include"child.h"

// 整型数字的比较函数
int compareInts(void *a,void *b){
int *ta = (int *)a;
int *tb = (int *)b;
return (*ta - *tb);
}

int main(){

// 创建对象
Person_T person = Person_new("mark",20);
Child_T child = Child_new("jack",21,178.0f);

// 通过对象名调用打印属性信息方法
person->showInfo(person);
child->person->showInfo(child);

// 通过对象名调用信息比较方法
person->compare(person->name,child->person->name,strcmp);
child->person->compare(&(person->age),&(child->person->age),compareInts);

// 打印比较结果
 printf("%d\n",person->compare(person->name,child->person->name,strcmp));
 printf("%d\n",child->person->compare(&(person->age),&(child->person->age),compareInts));

// 释放指针对内存的引用
 Person_free(person);
 Child_free(child);

return 0;
}

相关文章

  • (十九)面向对象编程

    无论C语言或是C++,Java,甚至脚本语言,汇编语言都能实现面向对象编程 面向对象设计过程 假设存在一个对象。要...

  • iOS Runtime

    Runtime背景 OC是基于C的一门面向对象的语言,C语言是一门面向过程的语言,面向对象的实现是通过C语言的一套...

  • (七)go语言面向对象

    面向对象 简介 go 语言中没有对象,和c语言类似,通过struct实现面向对象的程序编写,如下图:TreeNod...

  • 好好干

    C语言面向过程 C++面向对象

  • 重学iOS——1.从C角度看OC

    面向对象 面向对象的编程思维,简而言之就是:一切皆对象。面向对象的语言区别于C语言(面向过程),C语言是按照代码的...

  • 面向对象的基本概念

    面向对象的基本概念 1. 简述 C 和 C++ 有何不同? 宏观上: C 语言属于面向过程语言,通过函数来实现程序...

  • 3. Java面向对象编程

    类与对象[1] Java是面向对象的汇编语言(面向机器)—— c语言(面向过程)—— java语言(面向对象) 类...

  • Objective-C 语言特性之深入理解对象

    Objective-C 语言是一门面向对象的语言,Objectvie-C语言的底层是由c/c++和部分汇编语言实现...

  • 关于“C++面向对象高级编程上”第一次作业

    c语言面向过程,c++(以前为c with class)面向对象 面对对象的语言还有Java,C# 教学目的:良好...

  • 【Boolan】C++面向对象编程(上)-Clarence

    c语言面向过程,c++(以前为c with class)面向对象 面对对象的语言还有Java,C# 教学目的:良好...

网友评论

      本文标题:C语言实现面向对象

      本文链接:https://www.haomeiwen.com/subject/kpcenktx.html