#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),類的實(shí)例化
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),類的實(shí)例化
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)是最常用的設(shè)計(jì)模式之一,這種類型的設(shè)計(jì)模式屬于創(chuàng)建型模式,它提供了一種創(chuàng)建對象的(最佳)方式
網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)!專注于網(wǎng)頁設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、小程序制作、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項(xiàng)目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了善左免費(fèi)建站歡迎大家使用!在工廠模式中,我們在創(chuàng)建對象時(shí)不會(huì)對客戶端暴露創(chuàng)建邏輯,并且是通過使用一個(gè)共同的接口來指向新創(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)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級服務(wù)器適合批量采購,新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧
當(dāng)前名稱:Linux智能家居項(xiàng)目-創(chuàng)新互聯(lián)
轉(zhuǎn)載來于:http://jinyejixie.com/article46/dioshg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化、響應(yīng)式網(wǎng)站、網(wǎng)站收錄、全網(wǎng)營銷推廣、軟件開發(fā)、網(wǎng)站設(shè)計(jì)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容