网站建设完工报告那些网站建设的好
news/
2025/10/7 18:36:34/
文章来源:
网站建设完工报告,那些网站建设的好,博客网站设计及说明,深圳设计公司品牌1.什么是设计模式 软件设计模式#xff08;Design pattern#xff09;#xff0c;又称设计模式#xff0c;是一套被反复使用、多数人知晓的、经过分类编目的、代码设计经验的总结。使用设计模式是为了可重用代码、让代码更容易被他人理解、保证代码可靠性、程序的重用性。 …1.什么是设计模式 软件设计模式Design pattern又称设计模式是一套被反复使用、多数人知晓的、经过分类编目的、代码设计经验的总结。使用设计模式是为了可重用代码、让代码更容易被他人理解、保证代码可靠性、程序的重用性。 23种设计模式的介绍 设计模式通常描述了一组相互紧密作用的类与对象。
2.什么是类与对象
类类是一种用户定义的引用数据类型也称类类型每个类包含数据说明和一组操作数据或传递消息的函数对象类的一种具象实例化 C语言仿造类与对象的例子
#include stdio.hstruct Animal //用c语言仿照的动物类
{char name[12];int age; //成员属性char sex;void (*pRun)(); //成员方法void (*pEat)();
};void dogEat()
{printf(狗吃骨头\n);
}void catEat()
{printf(猫吃鱼\n);
}void dogRun()
{printf(狗会追着人跑\n);
}void catRun()
{printf(猫会爬树\n);
}int main()
{struct Animal dog{.pEatdogEat,.pRundogRun,}; //动物类实例化后的对象struct Animal cat{.pEatcatEat,.pRuncatRun,};/* dog.pEatdogEat;//为具体对象赋予行为方法dog.pRundogRun;cat.pEatcatEat;cat.pRuncatRun;*/dog.pEat();//调用具体对象的行为方法dog.pRun();cat.pEat();cat.pRun();return 0;
}3.什么是工厂模式
概念工厂模式Factory Pattern是 最常用的设计模式之一。这种类型的设计模式属于创建型模式它提供了一种创建对象的最佳方式。特点在工厂模式中我们在创建对象时不会对客户端暴露创建逻辑并且是通过使用一个共同的接口来指向新创建的对象。
用C语言采用工厂模式思想设计的例子 animal.h
#include stdio.hstruct Animal
{char name[12];int age; char sex;void (*pRun)(); void (*pEat)();struct Animal* next;};struct Animal* putDogInLink(struct Animal* phead);
struct Animal* putCatInLink(struct Animal *phead);cat.c
#include animal.hvoid catEat()
{printf(猫会吃鱼\n);
}void catRun()
{printf(猫喜欢爬树\n);
}struct Animal cat{//实例化对象猫.nameTom,.pEatcatEat,.pRuncatRun,};struct Animal* putCatInLink(struct Animal *phead)//将猫对象插入链表的方法
{if(phead NULL){pheadcat;return phead;}else{cat.nextphead;pheadcat;return phead;}}dog.c
#include animal.hvoid dogEat()
{printf(狗爱吃骨头\n);
}void dogRun()
{printf(狗会追着人跑\n);
}struct Animal dog{//实例化对象狗.namedahuang,.pEatdogEat,.pRundogRun,}; struct Animal* putDogInLink(struct Animal* phead)//将狗对象拆入链表的方法
{if(phead NULL){pheaddog;return phead;}else{dog.nextphead;pheaddog;return phead;}
}测试用的例子 mainPro.c
#include animal.h
#include string.hstruct Animal* findUnitByName(char *name,struct Animal *phead)//查找链表
{struct Animal *tmpphead;if(tmpNULL){printf(链表为空\n);return NULL;}else{while(tmp !NULL){if(strcmp(tmp-name,name) 0){return tmp;}tmptmp-next;}return NULL;}}int main()
{char buf[128]{0};struct Animal *phead NULL;struct Animal *ptmp NULL;phead putCatInLink(phead);phead putDogInLink(phead);while (1){printf(请输入想要查找的动物名字:Tom/dahuang\n);scanf(%s,buf);puts(buf);ptmpfindUnitByName(buf,phead);if(ptmp ! NULL){printf(%s\n,ptmp-name);ptmp-pEat();ptmp-pRun();}memset(buf,0,sizeof(buf));}return 0;
}设计思想图
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/930720.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!