PALM开发教程-第十二章 专业编程技巧 |
| 作者:palmheart 来源:palmheart.net 发布时间:2005-12-21 3:39:54 |
|
r );\ }\ return( NULL );\ } about窗体和prefs窗体的入口和calc窗体的模式相同,在main.c中不需要做其它的修改。 为Option菜单添加定义: //////////////////////////////// // Definitions for moptions.c // //////////////////////////////// // Menu ID name conversions #define OptionsAbout OptionsAboutCalculator 因为Calculator_res.h中的常量定义基于具体的菜单项,所以我们应该将其抽象为不含诸如“Calculator”等基于具体程序的文本的通用名称,使之能重复使用。 程序列表 下面是新的app.h的全部代码: #ifndef APP_H #define APP_H ////////////////////////////////////////////////////////////////////////////// // app.h // Definitions for the application. // Copyright (c) 1999, Robert Mykland. All rights reserved. ////////////////////////////////////////////////////////////////////////////// ////////////// // Includes // ////////////// #include <Pilot.h> // All the Palm includes #include "Calculator_res.h" // Resource definitions #include "fabout.h" // Definitions for the "about" form #include "fcalc.h" // Definitions for the "calc" form #include "fprefs.h" // Definitions for the "prefs" form #include "main.h" // Definitions for the main entry point #include "moptions.h" // Definitions for the "options" menu ////////////////////////////// // Definitions for fabout.c // ////////////////////////////// // The menu event handler macro for the "about" form #define aboutFormMenuEventHandler(spEvent) ///////////////////////////// // Definitions for fcalc.c // ///////////////////////////// // The menu event handler macro for the "calc" form #define calcFormMenuEventHandler(spEvent) \ {\ optionsMenuEventHandler( spEvent );\ } ////////////////////////////// // Definitions for fprefs.c // ////////////////////////////// // The menu event handler macro for the "prefs" form #define prefsFormMenuEventHandler(spEvent) //////////////////////////// // Definitions for main.c // //////////////////////////// // The prototype for getEventHandler() static FormEventHandlerPtr getEventHandler( Word ); // This creates the function getEventHandler, // which returns the event handler for a given form // in this application. #define EVENT_HANDLER_LIST \ static FormEventHandlerPtr getEventHandler( Word wFormID )\ {\ switch( wFormID )\ {\ case AboutForm:\ return( aboutFormEventHandler );\ case CalcForm:\ return( calcFormEventHandler );\ case PrefsForm:\ return( prefsFormEventHandler );\ }\ return( NULL );\ } // This defines the macro that initializes the app #define appInit() // This defines the macro that cleans up the app #define appStop() // This application works on PalmOS 2.0 and above #define ROM_VERSION_MIN ROM_VERSION_2 // Define the starting form #define StartForm CalcForm //////////////////////////////// // Definitions for moptions.c // //////////////////////////////// // Menu ID name conversions #define OptionsAbout OptionsAboutCalculator #endif // APP_H 调试 又到了调试的时候了,界面应该和以前相同。当你按下菜单栏上的prefs或about选项时,相应窗体就会跳出。当按下各自的OK按钮后,返回到calc窗体。 和以前一样,程序从PilotMain()开始,按照常规的路线向下运行,processEvent()产生的frmLoadEvent事件初始化了窗体的新的菜单栏。然后程序就进入无限循环来等待事件的触发。 如果你按下OK按钮,系统将发出警告声音。 面向对象的编程 面向对象编程的目的就是使代码更加容易被维护和重复使用。基于其它代码中的一段或变量,使用封装和抽象的方法,使bugs变得很明显。只要你弄懂各个代码部分之间的关系,就可以找到并修正这些bugs。代码的重用性提高是因为各个代码部分间的依赖性减少了,这样就可以从很容易的从面向对象的程序中摘出有用的代码应用到其它的工程中去。根据上个世纪中人们对面向对象编程的看法,或许你从认为它不过是骗局转变到认为它是唯一的编程方法吧。或许你是对的,也就是说,不管有没有价值,我也对面向对象的编程谈一下或许并不谦虚的看法。 首先,面向对象的编程应该是一种编程的设计原则,并不和你所使用的编程语言有多大的关系。的确,象C++和JAVA很容易实现面向对象的方法,但应用的便利应是面向对象这种原则所提供的。经过近十年的大量使用面向对象的语言诸如Smalltalk、C++和JAVA的编程,我知道了一个人如果精通JAVA的语法,并不代表他真正知道了如何有效的、有价值的利用对象。 真正重要的应该是理解面向对象的理念和设计模式,知道为什么使用这些理念和设计原则就更有效和更有价值。面向对象的编程也是一种工具,这和别的并没有区别,如果你胡乱用的话,它和其它工具的滥用一样会造成损害。没有人会因为锤子是最新的产品就在房间里乱砸东西。但是,我看到许多人由于使用面向对象的编程方法不对,结果弊大于利。 C++、JAVA和Palm OS 一般来讲,我也很喜欢使用C++和JAVA。但我不提倡在Palm OS中使用的原因是,这些语言的类库在Palm OS中的使用并没有带来任何益处。我们得到的只是程序更臃肿、运行速度更慢。 当有了一个小的、简洁为Palm OS或C++和JAVA开发的类库,我会第一个使用它。或许我将写一个。 幸运的是,在Palm OS应用程序的开发中,我们可以从面向对象的原则中获得一些有意义的启示。如我前面所言,关键就是掌握面向对象的原则。下面,我就向你介绍一下面向对象编程的一些最流行的概念,并给出一些如何在C程序使用这些原则的技巧。 数据封装 面向对象的基本原则就是将数据结构及其应用放在一个叫做“对象”的实体中。数据封装的思想就是把数据结构保护起来,以免遭到对象外代 |
| [] [返回上一页] [打 印] |
|
文章评论 |
