Linux智能家居項目-創(chuàng)新互聯(lián)

一、C語言的面向?qū)ο缶幊趟枷?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;//類的具象化表現(xiàn),類的實例化
	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
	};//類的具象化表現(xiàn),類的實例化
	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)是最常用的設計模式之一,這種類型的設計模式屬于創(chuàng)建型模式,它提供了一種創(chuàng)建對象的(最佳)方式

網(wǎng)站建設哪家好,找創(chuàng)新互聯(lián)!專注于網(wǎng)頁設計、網(wǎng)站建設、微信開發(fā)、小程序制作、集團企業(yè)網(wǎng)站建設等服務項目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了善左免費建站歡迎大家使用!

在工廠模式中,我們在創(chuàng)建對象時不會對客戶端暴露創(chuàng)建邏輯,并且是通過使用一個共同的接口來指向新創(chuàng)建的對象

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;
}

你是否還在尋找穩(wěn)定的海外服務器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機房具備T級流量清洗系統(tǒng)配攻擊溯源,準確流量調(diào)度確保服務器高可用性,企業(yè)級服務器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧

當前名稱:Linux智能家居項目-創(chuàng)新互聯(lián)
轉(zhuǎn)載來于:http://bm7419.com/article46/dioshg.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供關鍵詞優(yōu)化、響應式網(wǎng)站網(wǎng)站收錄、全網(wǎng)營銷推廣軟件開發(fā)、網(wǎng)站設計

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

搜索引擎優(yōu)化