189 8069 5689

Linux智能家居项目-创新互联

一、C语言的面向对象编程思想-OOP(Object Oriented Programming) 1、C语言中的“类” OOP1.C
#includestruct Animal{//类似与其他语言的抽象类概念
	char name[128];
	int age;
	char sex;
	void (*peat)();
	void (*pbeat)();
};

void dogEat()
{
	printf("狗吃屎\n");
}

void catEat()
{
	printf("猫吃鱼\n");
}

void personEat()
{
	printf("人吃米\n");
}

void dogBeat()
{
	printf("咬你小弟弟\n");
}

void catBeat()
{
	printf("抓你小弟弟\n");
}

void personBeat()
{
	printf("猴子偷桃\n");
}

int main()
{
	struct Animal dog;//类的具象化表现,类的实例化
	struct Animal cat;
	struct Animal person;

	dog.peat=dogEat;
	cat.peat=catEat;
	person.peat=personEat;

	dog.pbeat=dogBeat;
	cat.pbeat=catBeat;
	person.pbeat=personBeat;

	dog.peat();
	cat.peat();
	person.peat();

	dog.pbeat();
	cat.pbeat();
	person.pbeat();


	return 0;
}
OOP2.c 
#includestruct Animal{//类似与其他语言的抽象类概念
	char name[128];
	int age;
	char sex;
	void (*peat)();
	void (*pbeat)();
};

void dogEat()
{
	printf("狗吃屎\n");
}

void catEat()
{
	printf("猫吃鱼\n");
}

void personEat()
{
	printf("人吃米\n");
}

void dogBeat()
{
	printf("咬你小弟弟\n");
}

void catBeat()
{
	printf("抓你小弟弟\n");
}

void personBeat()
{
	printf("猴子偷桃\n");
}

int main()
{
	struct Animal dog={
		.peat=dogEat,
		.pbeat=dogBeat
	};//类的具象化表现,类的实例化
	struct Animal cat={
		.peat=catEat,
		.pbeat=catBeat
	};
	struct Animal person={
		.peat=personEat,
		.pbeat=personBeat
	};

	//dog.peat=dogEat;
	//cat.peat=catEat;
	//person.peat=personEat;

	//dog.pbeat=dogBeat;
	//cat.pbeat=catBeat;
	//person.pbeat=personBeat;

	dog.peat();
	cat.peat();
	person.peat();

	dog.pbeat();
	cat.pbeat();
	person.pbeat();


	return 0;
}
2、工厂模式

工厂模式(Factory Pattern)是最常用的设计模式之一,这种类型的设计模式属于创建型模式,它提供了一种创建对象的(最佳)方式

网站建设哪家好,找创新互联!专注于网页设计、网站建设、微信开发、小程序制作、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了善左免费建站欢迎大家使用!

在工厂模式中,我们在创建对象时不会对客户端暴露创建逻辑,并且是通过使用一个共同的接口来指向新创建的对象

mainpro.c
#include "Animal.h"
#includestruct Animal *find_LinkNode_Byname(struct Animal *phead,char *name)
{
	if(phead == NULL){
		return NULL;
	}
	while(phead !=NULL){
		if(strcmp(phead->name,name) == 0){
			return phead;
		}
		phead = phead->next;
	}
	return NULL;
}





int main()
{
	char buf[128]={0};
	struct Animal *phead =NULL;
	struct Animal *tmp =NULL;
	phead = add_Cat_In_Link(phead);
	phead = add_Dog_In_Link(phead);
	phead = add_Person_In_Link(phead);

	while(1){
		printf("input:Tom,huang,pengkaifan\n");
		scanf("%s",buf);
		tmp = find_LinkNode_Byname(phead,buf);
		if(tmp == NULL){
			printf("name is NULL\n");
			continue;
		}else{
			tmp->peat();
			tmp->pbeat();
		}
		
	}

	
	return 0;
}
Animal.h
#includestruct Animal{//类似与其他语言的抽象类概念
	char name[128];
	int age;
	char sex;
	void (*peat)();
	void (*pbeat)();
	struct Animal *next;
};

struct Animal *add_Cat_In_Link(struct Animal *phead);
struct Animal *add_Dog_In_Link(struct Animal *phead);
struct Animal *add_Person_In_Link(struct Animal *phead);
cat.c
#include "Animal.h"

void catEat()
{
	printf("cat eat fish\n");
}
void catBeat()
{
	printf("hit your brother\n");
}


struct Animal cat={
		.name="Tom",
		.peat=catEat,
		.pbeat=catBeat
};


struct Animal *add_Cat_In_Link(struct Animal *phead)
{
	if(phead != NULL)
		cat.next=phead;
	return &cat;
}
dog.c
#include "Animal.h"

void dogEat()
{
	printf("dog eat shit\n");
}
void dogBeat()
{
	printf("hit your brother\n");
}


struct Animal dog={
		.name="huang",
		.peat=dogEat,
		.pbeat=dogBeat
};


struct Animal *add_Dog_In_Link(struct Animal *phead)
{
	if(phead != NULL)
		dog.next=phead;
	return &dog;
}
person.c
#include "Animal.h"

void personEat()
{
	printf("person eat rich\n");
}
void personBeat()
{
	printf("hit your brother\n");
}


struct Animal person={
		.name="pengkaifan",
		.peat=personEat,
		.pbeat=personBeat
};


struct Animal *add_Person_In_Link(struct Animal *phead)
{
	if(phead != NULL)
		person.next=phead;
	return &person;
}

你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧


网站栏目:Linux智能家居项目-创新互联
当前链接:http://cdxtjz.cn/article/dioshg.html

其他资讯