提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
前言一、创建头文件二、创建.c文件1 cat.c2.dog.c3 person.c
三.创建main.c四.运行结果总结
前言
工厂模式:常用的设计模式之一。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。
在工厂模式中,我们在创建对象时不会对客户端暴露创建逻辑,并且是通过使用一个共同的接口来指向新创建的对象。
.h -> ①创建一个共用类(结构体)②声明函数 .c ->对类具体化(对象)
围绕着结构体来编程,将变量,函数都存放于结构体当中,然后通过链表将多个结构体(.c文件)连接起来,即构成一个项目。
一、创建头文件
animal.h
#include <stdio.h>
struct Animal
{
char name
[128];
int age
;
int sex
;
void (*peat
)();
void (*pbeat
)();
struct Animal
*next
;
};
struct Animal
*putCatInLink(struct Animal
*phead
);
struct Animal
*putDogInLink(struct Animal
*phead
);
二、创建.c文件
1 cat.c
#include "animal.h"
void catEat()
{
printf("猫吃鱼\n");
}
void catBeat()
{
printf("抓你小弟弟\n");
}
struct Animal cat
= {
.name
= "TOM",
.peat
= catEat
,
.pbeat
= catBeat
};
struct Animal
*putCatInLink(struct Animal
*phead
)
{
if(phead
== NULL){
phead
= &cat
;
return phead
;
}else{
phead
->next
= &cat
;
cat
.next
= NULL;
return phead
;
}
}
2.dog.c
#include "animal.h"
void dogEat()
{
printf("狗吃屎\n");
}
void dogBeat()
{
printf("咬你小弟弟\n");
}
struct Animal dog
= {
.name
= "taidi",
.peat
= dogEat
,
.pbeat
= dogBeat
};
struct Animal
*putDogInLink(struct Animal
*phead
)
{
if(phead
== NULL){
phead
= &dog
;
return phead
;
}else{
phead
->next
= &dog
;
dog
.next
= NULL;
return phead
;
}
}
3 person.c
#include "animal.h"
void personEat()
{
printf("人吃米\n");
}
void personBeat()
{
printf("猴子偷桃\n");
}
struct Animal person
= {
.name
= "qiuyiguang",
.pbeat
= personBeat
,
.peat
= personEat
};
struct Animal
*putPersonInLink(struct Animal
*phead
)
{
if(phead
== NULL){
phead
= &person
;
return phead
;
}else{
phead
->next
= &person
;
person
.next
= NULL;
return phead
;
}
}
三.创建main.c
#include "animal.h"
#include <string.h>
struct Animal
*getName(char *str
,struct Animal
*phead
)
{
struct Animal
*tmp
= phead
;
if(phead
== NULL){
printf("空\n");
return NULL;
}else{
while(tmp
!= NULL){
if(strcmp(str
,tmp
->name
) == 0){
return tmp
;
}
tmp
= tmp
->next
;
}
}
}
int main()
{
char buf
[128] = {'\0'};
struct Animal
*phead
= NULL;
struct Animal
*ptmp
;
phead
= putCatInLink(phead
);
phead
= putDogInLink(phead
);
phead
= putPersonInLink(phead
);
while(1){
printf("请输入TOM,taidi,qiuyiguang:\n");
scanf("%s",buf
);
ptmp
= getName(buf
,phead
);
if(ptmp
!= NULL){
ptmp
->peat();
ptmp
->pbeat();
}
memset(buf
, '\0', sizeof(buf
));
}
return 0;
}
四.运行结果
总结
提示:这里对文章进行总结: 例如:以上就是今天要讲的内容,本文仅仅简单介绍了pandas的使用,而pandas提供了大量能使我们快速便捷地处理数据的函数和方法。