C语言面向对象编程

C语言面向对象编程

什么是面向对象

为了说明C语言也可以面向对象编程,有必要说一下面向对象中的几个概念:

可以看出,面向对象只是一种思想,与具体语言无关,只要实现了这几条就是所谓的面向对象了。

看具体代码:

#include <stdio.h>#include <stdlib.h>typedef struct _CClass{_CClass *self; a;int b;(*print)(void *self);} CParent;#define EXTERND_CLASS_FROM_CParent \void (*print)(void *self); \int a;\int b;\typedef struct _CChild{//继承CParent EXTERND_CLASS_FROM_CParent//添加属性 CParent parent;int c;int d;(*sayHello)();} CChild;void print(void *self);void sayHello();int main(int argc, char const *argv[]){CParent *parent=(CParent *)malloc(sizeof(CParent));//为属性赋值parent->a=1;parent->b=2;parent->print=print;//调用方法parent->print((void *)parent);free((void *)parent);//继承CChild *child=(CChild *)malloc(sizeof(CChild));child->a=3;child->b=5;child->print=print;child->sayHello=sayHello;child->print((void *)child);child->sayHello();free((void *)child);//多态CChild *child1=(CChild *)malloc(sizeof(CChild));CParent *parent1=(CParent *)child1;parent1->a=5;parent1->b=6;parent1->print=print;parent1->print((void *)child1);free((void *)child1);return 0;}void print(void *self){CParent *tmp=(CParent *)self;printf(,tmp->a,tmp->b);}void sayHello(){printf();}

面向对象从来都是思想,美国空间,而不是语言!

posted on

,网站空间,美国空间婚姻犹如一艘雕刻的船,看你怎样去欣赏它,又怎样驾驭它。

C语言面向对象编程

相关文章:

你感兴趣的文章:

标签云: