自学习软件以来,先后使用c ,vfp6.0 ,vb 6.0 ,java ,c# 开发软件做项目,现在的项目又回到学习的第一门语言c上来,现在的项目用嵌入式非标准c语言做开发,由于项目较大,需要实现例如线程,网络,日历,各种主流存储卡的操作等方面,所以写出高效简洁易理解的程序成为开发组成员的长期原则。
今天做一个显示菜单,因为不象高级语言一样,有直接的控件可以用,很快搞定,必须自己写结构并实现,显示在外接大屏幕上。因为上一个项目是用c#开发的,所以在做这个设计的时候可能就不自觉的用了一些思想,等到写完了主要的几个文件[menu.h ,menu.c]后发现我的着几个文件简直可以稍加变动移植成为c#,并且即便在c#里还不算很烂的设计,进一步看,然后给同事调用,调试修改错误等,证明了模块的独立性非常好,因此在此做一比较分析。
menu.h文件如下:(menu.c省略,为个函数实现)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
//此处也可用enum
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
对应可转换为c#的一个接口(IMenu.cs)和实现类(Menu.cs)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
在menu.c中的代码中将指针改为对应对象(在c中是为了节约资源),就很快成为class Menu了。
在计算机语言中,面向对象的类一般具有三个基本特性:封装性、继承性和
多态性。
现在来看一下是如何借鉴OO思想的 (显示封装性,提高程序模块化和阅读性):
1。 分为.h和.c来写(也许你回说在c中本来就一直这么做的,但很多人在.h中的定义并不好),和OO中的接口的概念相似,定义和实现分开,并且对外提供接口,提高可维护性。
2。详细的看.h的定义。
menu.h可以看做一个c#文件,包含几个类的定义
第一段,enum定义保证在引用是不使用绝对数,用有意义的字符出现在程序中增加可读性。stuct结构可以是看作一个类。
struct结构的定义和作用就和c#中完全一样了。
在看对应class Menu的定义,最开始可以认为是属性申明(public和protect类型的),中间是是属性的操作(在java中对应get_*和set_*操作,c#中则可以用get / set 方法进一步封装)当然在c中也可以来通过函数来访问,但因为在c中他们不论怎么定义都必须是全局变量,所以空间必须被占用的,而增加一个函数来访问就降低了效率,也占用了代码空间和cpu资源。最后的部分是类的操作。
如果要修改系统主菜单,显然只修改menu.c中的具体text显示就可以了,操作方式或流程,输出对象改变了,也可以只修改menu.c 文件,而调用和接口保持不变。
按照模块化的思想,这个程序达到了较高的模块独立性,内部menu.c的函数实现和外部调用也可以达到比较满意的高聚合和低耦合。
作为软件发展的较高阶段的OO思想,指导其产生的土壤,也可以提高我们不得不用c语言和其他较低级和非面向对象语言编程的场合,提高大型项目的软件编写效率和质量。