#include <stdio.h> #include <stdlib.h> /*用c语言模拟面向对象操作 修改:张亚雄 C语言中的结构体是后来面向对象编程中的类的启蒙师傅。 只不过很遗憾,丹尼斯忘记把函数放进结构体里面了。要不然后来也就没有java和C#什么事了。 对象具有属性和方法,一般用类来来封装属性和方法。 C语言是面向过程的编程语言,那么,它能不能借用面向对象的思路来进行程序设计呢?答案是可以。 这里尝试使用结构体和指针来实现这个想法。不过实例一只实现了属性的封装。该程序正常运行 实例一 先定义一个结构体 */ struct Person { char* name; int age; int height; int weight; }person,*p; //并没有使用,思考为什么? /* 下面是一个造人的函数,可以存储对象的属性,但是还没有方法。返回的是一个结构体指针变量。造人函数有4个参数。 这些参数传递给内部的结构体成员进行赋值,这相当于给对象的属性赋值。 这个结构体变量在函数中被maloco函数配置到了内存的一个区域, maloco函数会返回一个指针给结构体指针变量who。 在函数内部使用这个结构体指针变量who给结构体成员赋值。 然后把这个指针变量返出函数外部。 */ struct Person* Create_person(const char* name,int age,int height,int weight) { struct Person* who = malloc(sizeof(struct Person)); /*用内存分配函数maloco开辟一块区域,大小由关键字sizeof决定 */ /*该malodo函数会返回一个无类型指针变量*/ /*把malodo函数返回的无类型的指针赋值给结构体指针变量who。赋值的同时会进行强制类型转换。*/ /*这一个表达式包含了很多知识点,能看懂的童鞋说明有一定的功力*/ who->name=name; //给结构体内部的成员赋值 who->age=age; who->height=height; who->weight=weight; return who; //返回一个指针 } //当调用上面这个函数的时候,通过参数把数据传递进去,感觉像是给类的属性赋值。 //怎么“杀人”呢? void Destroy_person(struct Person* who) { free(who->name); // be carefull ,只杀一个姓名? free(who); //通过指针释放这个内存区域 } //输出“人”的信息的函数: void Print_person(struct Person* who) { printf("Name:%s\n",who->name); printf("Age:%d\n",who->age); printf("height:%d\n",who->height); printf("weight:%d\n",who->weight); } //main.最后测试一下: int main(int argc, char *argv[]) { // make two people structures 造两个人:joe 和frank。通过参数赋值。 struct Person *joe = Create_person( "Joe Alex", 32, 64, 140); //给joe初始化赋值,数据被保存在一个结构体里 struct Person *frank = Create_person( "Frank Blank", 20, 72, 180); //可以用这个表达式不断地造人 printf("c语言模拟面向对象编程\n"); // print them out and where they are in memory printf("Joe is at memory location %p:\n", joe);//打印joe的地址,注意%p符号。 Print_person(joe); //打印joe的数据 printf("**********************************\n"); printf("Frank is at memory location %p:\n", frank); //打印joe的地址,注意%p符号。 Print_person(frank); printf("*修改数据 and print them again*\n"); // make everyone age 20 years and print them again //修改joe的数据 joe->age += 20; joe->height -= 2; joe->weight += 40; Print_person(joe); //再次打印joe的修改过的数据 printf("***再次打印joe的修改过的数据***\n"); frank->age += 20; frank->weight += 20; Print_person(frank); printf("**再次打印frank的修改过的数据******\n"); // destroy them both so we clean up Destroy_person(joe); //杀死joe。真实的含义是释放内存 Destroy_person(frank); //杀死frank printf("space is free\n"); //可以添加检查指针是否为空的语句。 return 0; }
| 下面看一下我自己的实现方法:
文件 User.h
void PersonFunction();
文件 User.c
#include "User.h" #include<stdio.h> #include<limits.h> #include <stdlib.h> /* 猜想验证2:造一个假的类 思考: 类有哪些特征? 1. 有成员变量 2. 有行为 3. 有构造方法 思路: 用C里面的结构体来做 补充: 研究一下C里面的不定参函数 */ //结构体声明 typedef struct { int a; int b; void (*PersonFunctionPointer)(void);//方法指针 }Person; Person* Person_(int a, int b);//相当于构造方法 Person* Person_(int a, int b) {//相当于构造方法 Person* person = (Person*)malloc(sizeof(Person)); person->a = a; person->b = b; person->PersonFunctionPointer = &PersonFunction; return person; } bool freePerson(Person* person) {//对象销毁的方法 free(person); person = NULL; return person == NULL ? true : false; } void sayHi(Person* person) { printf("Hi I am Person.sayHi()...\n"); printf("a is:%d\n", person->a); printf("b is:%d\n", person->b); } int main() { Person* person = Person_(1, 1); person->a;//相当于成员变量 person->PersonFunctionPointer();//相当于成员方法 printf("free之前这个指针的值是:%p\n", person); freePerson(person); printf("free之后这个指针的值是:%p\n", person); getchar(); return 1; } void PersonFunction() { printf("大家好,我是PersonFunction\n"); }