的毕业设计论文; 2(包括毕业设计论文、源程序等内容在内的毕业设计电子文档及其它相关材料。
第3页
职场大变样社区(www.zcdby.com):下载毕业设计成品
主要参考文献(资料):
[1] 张秀香.基于Android的健康管理系统客户端的设计与实现[D].大连理工大学,2012. [2] 王克峰.基于Android的信息推送管理系统的设计与实现[D].大连理工大学,2012. [3] 邵翔,徐日.基于Android的事务管理型应用软件的设计与实现[J].北京电子科技学院
学报,2011, 19(4).
[4] 徐孝成.基于Android的密码管理系统的设计与实现[D].北京林业大学,2013. [5] 李家科.Android系统分析与开发[D].兰州交通大学,2014.
[6] 吴想想.基于Android平台软件开发方法的研究与应用[D].北京邮电大学,2011. [7] Conti J P.The Android are coming.Engineering & Technology,2008,3(9):153~156. [8] Haitham AL-Hajri,Krishnum Sasurooah.Iphone Forensic Methodoligy and
Tools[J].Proceedings if the 6th Australian Digital Forensic
Conference,2008:4-18.
[9] Yonghong Wu, Jianchao Luo, Lei Luo.Porting mobile web application engine to
the Android platform.2010 10th IEEE International Conference on Computer and
Information Technology (CIT 2010),2010.
[10] Jerry Gao,Mansi Modak,Satyavathi Dornadula, et al.Mobile Jabber IM: A
Wireless-Based Text Chatting System,2004.
专业班级 学生
要求设计(论文)工作起止日期
指导教师签字 日期
教研室主任审查签字 日期
系主任批准签字 日期
职场大变样社区(www.zcdby.com):下载毕业设计成品
基于安卓的女性生理健康管理系统的设计与实现
摘要
21世纪的今天,是科技融入生活方方面面的时代。IOS以及Android 等手机系统开发应用的实用性与适用性在告诉发展,各类与人们健康相关的app也随处可见。女性生理健康的管理系统的开发也是必要的。
关于女性生理健康管理的app是专为女性朋友或是有女朋友的男性同胞所准备的。本app实现了最基本的登陆、记录、倒计时的功能,虽然功能并没有市场上成熟的app强大,但是有了该app的核心功能。该app用eclipse作为开发工具,使用java编程语言来完成。
本篇论文,就基于安卓的女性健康管理系统的开发过程、操作步骤及核心技术进行论述,通过对app开发的需求分析、详细设计、设计实现来对该app开发及实现进行完整的介绍。本文的论述详细介绍了整个app的开发流程。
关键字: 女性生理健康管理系统;安卓;eclipse
职场大变样社区(www.zcdby.com):下载毕业设计成品
Design and Implementation of Andrews Female
Physiology and Health Management System Based on
Abstact
The 21st century is the era of technology into all aspects of life. A practicability and applicability IOS and other mobile phone system development and application of telling development, all kinds of people with health-related app everywhere. Development of women's physical health management system is also necessary.
App on women's physical health management is designed specifically for a female friend or a girlfriend male counterparts prepared. This app implements basic landing record, countdown function, although the function is not a mature market powerful app, but with the app's core functionality. The app with eclipse as a development tool, using java programming language to complete.
This paper, based on the development of women's health Andrews management systems, procedures and core technologies discussed by analyzing the demand for app development, detailed design, design app development and implementation to achieve the complete description. This article discusses in detail the entire app development process.
Keywords: women's physical health management system ; Android ; eclipse
职场大变样社区(www.zcdby.com):下载毕业设计成品
目录
1绪 论........................................................................................................................................ 1 1.1 系统开发的目的 ............................................................................................................. 1 1.2 开发的意义 .................................................................................................................... 1 1.3 创新之处 ........................................................................................................................ 1 1.4开发的背景 ..................................................................................................................... 2
2 需求分析.................................................................................................................................. 4 2.1 功能需求 ........................................................................................................................ 4 2.2 android网关设计
的C系统函数数据库,它是一个基于嵌入式linux的设备。
?FreeType:矢量、位图字体显示。
?SQLite:所有的应用程序都能使用的强大的数据库引擎。
2.3.4 android运行库
Android里有一个核心库,这个核心的运行库给整个Android的所有应用程序提供了Java的编程语言提供很多强大的多数功能。每一个Android的App应用都是在它本事应用的进程中运行,它们都拥有自己的独立的虚拟机实例。这个虚拟机被弄成了一个可以同时运行多个虚拟机的高效平台系统。
2.3.5 Linux 内核
Android 的核心服务系统依赖于Linux 2.6 内核,比如它所需要的安全性,内存管理,进程管理,网络协议栈和驱动模型等,这些都是linux内核所提供的。Linux内核也是被同时当成硬件和软件栈之间的抽象层。
2.4 java语言介绍
2.4.1 语言特点
(1)面向对象
面对对象其实就是把整个应用程序系统看成是各种各样对象的集合,对象就是这个系统中最小的子系统,然后一些相关的对象就能因此组合成为更加复杂一些的子系统。面向对象的开发应用程序,就是把所要开发的软件应用程序系统看成是各种对象的结合,这样就比较贴合人的自然思维。对象之间通过的消息都是相互作用的。面向对象与面向过程的区别就在于操作的对象不一样,面向对象就是指对对象进行操作,不管过程如何,只在对象上做改变。面向过程则是不管对象,指在对象与对象间操作的过程中进行操作。对象就是对问题领域中对所有事件的抽象表达。万物皆可为对象。而且每个对
6
职场大变样社区(www.zcdby.com):下载毕业设计成品
象都是唯一的,并且每个对象都有其特定的属性和行为。
(2)平台无关性
Java的平台无关性是指用Java语言所写的应用程序,在任何平台都可以使用,它是不挑设备,在任何设备上都不需要修改原本的程序代码。但是C与C++在不同平台上不是都能使用的,在不同的平台上还需要有专门的编译语言去化解,才能使用。
(3)分布式
分布式包含两种形式,一时数据的分布,一种是操作的分布。数据的分布与操作的分布式不同的,主要区别在于数据的分布是将数据分布在不同的主机或设备上,操作的分布则是将计算分布在不同的主机或舌部
)可靠性和安全性 (4
由Java的发展史可以知道,它最初被设计的目的就是为了填补C++所不能的做的事情,是电子类的设备,所以在可靠性的要求也是很高的。虽然Java源于C++,但是他比C++更加可靠,消除了C++所存在的很多不可靠因素,并且可以防止许多的编译错误的发生。这些可以分为四点来讲。首先是java属于强类型的语言,这就需要可以看得见的显示声明方式,这可以确保编译器能及时的发现方法调用所发生的错误,这样就能保证程序更加可靠;然后是,Java与C、C++最大的区别是,Java没有指针,这防止内存的非法访问;三是,Java有防止内存丢失等内存分配发生的自动单元收集功能;最后是Java的解释器,当它运行的时候,可以实施发现数组与字符串访问越界的功能。
(5)多线程
线程是操作系统的一个概念,它又被称作轻量进程,是比传统进程更小的可并发执行的单位。多线程就是指多个进程共同执行。
2.4.2 J2ME定义
J2ME是专门针对移动设备和PDA或者其他小型的设备所使用的Java语言。它是一种新型的比较小的Java程序应用的运行环境,它的整个构架主要是为了推广在手持式的小型设备中使用的一种Java技术。J2ME有CLDC与CDC两种设备。其中CLDC是为了一些比较小型的设备,一般是那种CPU与内存相对于标准来说很低的手机或者其他手提设备所提供的配置,它其实就是对Java的标准版在最大程度上进行缩减所得到的,它所拥有的虚拟机与核心库也就是100KB左右。而CDC则是为了可能会拥有更
7
职场大变样社区(www.zcdby.com):下载毕业设计成品
大资源的下一代设备而最大程度上扩大的Java标准版。CLDC配置则给各种各样的手持设备提供了能运行Java程序的平台环境,但是这个配置开发者是很难用其进行开发。J2ME平台是移动设备配置文件与在MIDP之上所建立的可选性软件包。 2.4.3 J2ME总体架构
J2ME为了支持其资源受限设备所要求的灵活性与可制作性,将其自身的体系分为了如图所示的5层。由图可以看出其中配置层所定义的Java虚拟机需要运行在设备的操作系统之上,然后构成整个平台的基础。框架层在其中的作用是为提供针对各种设备的各种特殊功能的API和扩展类库,下来的配置层则是为其提供了基本的语言特性。
框架层,MIDP,
配置层
J2ME虚拟机
操作系统
J2ME的体系结构
图2.4.3 J2ME总体架构
2.4.4 关于eclipse
Eclipse是一个基于Java语言的可扩展的开发平台,主要用于各种应用的源代码开发。Eclipse它本事只是一个框架和一组响应的服务,并不能直接开发什么程序。在Eclipse中几乎每个东西都是插件。所以在应用Eclipse是需要构建与其环境能紧密集成的工具,所以在搭建开发环境的之后,可以通过下载各种插件比如Jsp插件、Tomcat插件等进行整合,这样也可以用于JSP或者J2EE的开发。
8
职场大变样社区(www.zcdby.com):下载毕业设计成品
2.5 接口分析
2.5.1 用户接口
a. 与用户的直接接触,所有的屏幕都是用户的接口,所有的界面都需要尽量保持整洁且简单,必要的时候需要有提示;
b(保证反应的及时性,即每个功能到下一个功能的转换都需要流畅且保证转换时间的及时与快速;
c(保证程序的各个功能模块的可用性与各功能的流畅转换;
这里的输入包含两个方面,一个是按钮的点击,这个包含了手指的滑动与点击,另一个则是文本的输入。整个过程都需要系统对他们确认后并且对他们进行及时的响应;输出则包含获得信息的显示等。
2.5.2 硬件接口
A(硬件接口:所有的App都是可以基于所有的Android系统平台的手机或者其他设备。
B. 网络硬件接口要求,由于本系统并不需要实现联网的功能,所以并无此方面的要求。
2.5.3 软件接口
在我开发的App应用程序中它所包含的软件接口有历史数据与系统的接口;还有就是在整个开发过程中预想功能的实。如果需要用到系统与地图的联系等,那该App应用程序还应该拥有可以链接到用户信息的系统。
2.6开发环境选择
操作系统:Microsoft Windows 8
程序语言:Java
开发包:Sun Java (TM) Wireless Toolkit 2.5.2 for CLDC
9
职场大变样社区(www.zcdby.com):下载毕业设计成品
Eclipse SDK Version: 3.3.1.1
Java(TM) 2 Standard Edition(jdk1.5.0_14)
IDE:eclipse 3.3.1.1
整个App开发所需要的环境,整合了J2DK+Eclipse+NLpack-eclipse+
Eclipseme+Wireless Tool Kit,整个环境在安装配置过程也比较简单,在此就详细的阐述了。可以这么说,JDK是整个运行应用程序的虚拟机,而WTK则是相当于了仿真器,Eclipse及其它插件提供了开发测试程序代码的IDE集成开发环境。此外,这些所需要的软件及环境,都是可以从网上直接下载的。
10
职场大变样社区(www.zcdby.com):下载毕业设计成品
3 概要设计
3.1 功能模块设计
系统具有功能思路划分如下:
(1)本次所开发的App所需要的数据不需要后代支持使用数据库,在整个系统的中所涉及的数据不需要太高的安全保证,因为本身数据量就比较小,数据结构相对其他的应用系统的结构相对简单。所以在系统中数据获取使用的XML文档实现,是从服务器上下载的格式化后的XML文档,然后解析程序之后得到的。目前XML已经变成一种常见且通用的数据获取且传递的格式,它的平台的多方面无关性,使得数据的集成与交互更加的方便。
(2)XML解析: SAX和DOM是现在比较主流的两种解析方式。就Android系统来讲由于内存限制的原因,SAX的XML解析方式比较适合。
)数据显示:通过从服务器端获取再由xml解析出来的数据通过图形图像图表(3
的形式表示。
(4)数据存储:将服务器端的XML文档存之手机的内存或者手机的存储卡中,形式采用几个xml文件。
3.1.1 系统的模块设计
功能需求主要从几个方面下手,第一也是最基本的信息获取界面,即初始化话界面,这个界面就是将最基本的信息获取。然后是登陆界面,以及登陆进去的主界面,主界面就是有基本信息的统计,然后是能进行修改密码的修改界面,以及最后的程序说明界面。退出确定界面以及开始界面,这是我目前需要做的功能。还有就是进入界面所有的进入界面。但主要的还是倒计时的功能。
以下是系统功能模块图:
11
职场大变样社区(www.zcdby.com):下载毕业设计成品
图3.1.1功能模块图
3.1.2 模块的具体功能
登陆界面,整体设计思路就是按照现有的App的格式来设计实现的,App的开始会让用户登录,如若没有账户,则会提醒用户注册。若用户已经注册后,则会跳回登录界让用户登录。注册界面则为用户的信息获取,包括其需要的昵称、用户名、密码,这是使用该App最基本的要求。
快速设定界面是仿照美柚的App所写的,并不是纯粹的模仿,而是觉得这个界面确实需要并且美柚App的这个界面写个很不错,所以拿过来借鉴一下。这个界面的作用在于,记录并存储一下用户的经期数据,比如上一次经期的时间,与本人的经期周期时间,这个记录有利于我们后期对经期时间的预计与离下次经期时间的计算,以便更直观的告诉用户。
用户设置界面主要功能分为三部分,分别为账户设置,这里的账户设置有更改密码更改周期的功能,点击进去之后分别会跳转到更改密码与更改周期的界面,这两个界面的主要功能与实现跟之前的注册界面与快速记录界面是差不多一样的,关于里写着跟本App相关的内容,退出就是退出的按钮,点击退出会将整个程序退出。
日记记录界面的主要功能是发表心情,可以记录一天的心情,每天记录的心情会存到数据库里,然后发布到整个日记界面,你可以可以查看每天的心情,当你觉得不要的时候可以实行删除功能。这个日记记录界面包含标题,与内容两个部分,基本与市场上的此功能一样。整体还是挺好的。与之相呼应的还有查看日记的功能就是在日记列表里点击日记,就可以查看该日记的内容。
12
职场大变样社区(www.zcdby.com):下载毕业设计成品
4 详细设计
在概要设计的基础上进行了详细设计,详细设计就是细化了到每个模块,每个界面,每个界面需要用到什么技术等,还包括一个数据库与数据库表的设计 4.1 功能模块的具体设计
整个App根据之前的功能模块图都有一个大概的设计了解。这个里面的设计就体现在一些方面。
比如第一个登陆界面就是想像一个普通的App一样实现,在这里借鉴了一些别人的界面设计,最后有了一个呈现。其实接下来的很多界面大多都是参照的世面上的App所进行修改的。这些我就不多做解释。
在整个App中主要设计贡献就是在日记模块这里,在这里我用了一个下拉刷新组建和一个google最新发布的悬浮button组建,这些虽然是用的别人的组建。在日记删去与查看也是用了点击与长按的功能,整个日记设计模块实现的还是挺不错的。 4.2数据持久化的设计
整个系统的数据持久化我用了两种方式:一种是SharedPreferences,一种是sqlite数据库。
4.2.1 SharedPreferences
SharedPreferences是Android平台上一个轻量级的存储类,用来保存应用的一些常用配置,比如Activity状态,Activity暂停时,将此activity的状态保存到SharedPereferences中;当Activity重载,系统回调方法onSaveInstanceState时,再从SharedPreferences中将值取出。
在整个数据化持久的实现中我在SharedPreferences里存放的是获取的用户名、日期、周期等基本信息,本身是想都用SharedPreferences来存储与实现,但是由于日记功能里有一个查找、删除,这个存储相对于SharedPreferences来说,sqlite能更好的实现,因为sqlite数据库有建表比较容易操作。
13
职场大变样社区(www.zcdby.com):下载毕业设计成品
4.2.2 SQLite
为了能更加便利地管理数据库,Android提供了一个SQLiteOpenHelper帮助类,有了这个类我们就可以方便地对数据库进行创建和升级。SQLiteOpenHelper是一个抽象类,所以在使用它的时候,我们就需要创建一个个人的帮助类去继承它。 SQLiteOpenHelper中有两个抽象方法,分别是onCreate()和onUpgrade(),我们必须在个人的帮助类里面重写这两个方法,然后分别在这两个方法中去实现创建、升级数据库的逻辑。
SQLiteOpenHelper中还拥有两个特别重要的实例方法,一个事
getReadableDatabase()另一个则是getWritableDatabase()。这两个方法都可以创建或打开一个现有的数据库(若数据库已存在则直接打开,否则需要创建一个新的数据库),并且要返回一个可对数据库进行读写操作的对象。不同的是,当数据库不能写入的时候(如磁盘空间已满)getReadableDatabase()方法返回的对象将会以只读的方式去打开数据库,而getWritableDatabase()方法则将出现异常。
SQLite是一款轻量级的关系型数据库,它的运算速度非常快, 占用资源很少,通常只需要几百 K的内存就足够了,因而特别适合在移动设备上使用。SQLite 不仅支持标准的 SQL语法,还遵循了数据库的 ACID 事务。SQLite 又比一般的数据库要简单得多,它甚至不用设置用户名和密码就可以使用。Android 正是把这个功能极为强大的数据库嵌入到了系统当中,使得本地持久化的功能有了一次质的飞跃。
整个系统用的sqlite之建了一个表,就是一个简单的日记表,这个表里只有四个字段,包括id、时间、内容、标题。由于整个整个数据库就只有一个表,所以就在这里不具体的体现了
14
职场大变样社区(www.zcdby.com):下载毕业设计成品
5 功能实现
在管理信息系统的生命周期中,仅过了需求分析、系统设计等阶段之后,便开始了系统实施阶段。在系统分析和设计阶段,系统开发工作主要是集中在逻辑、功能和技术设计上,系统实施阶段要继承此前面各个阶段的工作成果,将技术设计转化为物理实现,因此系统实施的成果是系统分析和设计阶段的结晶。
在整个实现的过程中需要开发者的细心编程,在整个编程过程中要考虑到所有的情况,准备随时应对可能发生的各种状况,并且要以写出一个让用户满意的,并且符合用户需求的App,所以实现用户界面的整洁性,便捷性与快速性,成为整个开发过程中索要考虑并且特别重要的事情,如何做到这些,并且尽可能的优化各种功能都变的非常的重要且必要。设计不仅需要切合实际情况也要达到多需要的功能需求等,如何在编码的过程中能尽可能的贴合需求与预想出现的界面等是非常重要的,这都是考察程序员的功底与能力的时侯,需要特别的注意与小心。
5.1 登录注册界面
登陆界面,整体设计思路就是按照现有的App的格式来设计实现的,App的开始会让用户登录,如若没有账户,则会提醒用户注册。若用户已经注册后,则会跳回登录界让用户登录。注册界面则为用户的信息获取,包括其需要的昵称、用户名、密码,这是使用该App最基本的要求。
图5.1.1 登录界面 图5.1.2 注册界面 登录界面的主要代码:
15
职场大变样社区(www.zcdby.com):下载毕业设计成品
注册界面的主要代码:
18
职场大变样社区(www.zcdby.com):下载毕业设计成品
5.2 快速设定界面
快速设定界面是仿照美柚的App所写的,并不是纯粹的模仿,而是觉得这个界面确实需要并且美柚App的这个界面写个很不错,所以拿过来借鉴一下。这个界面的作用在于,记录并存储一下用户的经期数据,比如上一次经期的时间,与本人的经期周期时间,这个记录有利于我们后期对经期时间的预计与离下次经期时间的计算,以便更直观的告诉用户。
图5.2.1 快速登陆界面
快速登陆界面所涉及的核心代码:
20
职场大变样社区(www.zcdby.com):下载毕业设计成品
5.3用户设置界面
用户设置界面主要功能分为三部分,分别为账户设置,这里的账户设置有更改密码更改周期的功能,点击进去之后分别会跳转到更改密码与更改周期的界面,这两个界面的主要功能与实现跟之前的注册界面与快速记录界面是差不多一样的,关于里写着跟本App相关的内容,退出就是退出的按钮,点击退出会将整个程序退出。
图5.3.1 用户设置界面
用户设置界面对用的主要代码:
5.4 日记记录界面
日记记录界面的主要功能是发表心情,可以记录一天的心情,每天记录的心情会存到数据库里,然后发布到整个日记界面,你可以可以查看每天的心情,当你觉得不要的时候可以实行删除功能。这个日记记录界面包含标题,与内容两个部分,基本与市场上
23
职场大变样社区(www.zcdby.com):下载毕业设计成品
的此功能一样。整体还是挺好的。与之相呼应的还有查看日记的功能就是在日记列表里点击日记,就可以查看该日记的内容。
图5.4.1日记记录界面
日记记录界面的相关代码:
5.5 主界面
这是进入程序之后的主界面,在这个界面上有排卵期的倒计时,与周期时间是多少,还有离下次经期还有多少天。这里的数据都是根据之前快速设定界面所获取的数据,进行计算的。
图5.6.1 登陆以后的主界面
主界面相关的主要代码:
private void getNowDay(){
Calendar c = Calendar.getInstance();
25
职场大变样社区(www.zcdby.com):下载毕业设计成品
c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
int day = c.get(Calendar.DAY_OF_MONTH);
SharedPreferences sharedPreferences = getActivity().getSharedPreferences("menses", C
ontext.MODE_PRIVATE); //私有数据
day3 = sharedPreferences.getInt("distance",1);
begin = 28-(day - sharedPreferences.getInt("day",1));
}
private void initView(){
getNowDay();
dayBegin = (TextView)view.findViewById(R.id.home_begin);
dayBegin.setText("离下次排卵日还有 "+1+"天\n\n我的经期周期是"+day3+"天\n\n
离下次月经期还有+"+begin+"天");
}
26
职场大变样社区(www.zcdby.com):下载毕业设计成品
6 系统测试
程序整个设计过程中,系统测试的存在是很必要的。这是检验一个程序在运行过程中会不会出现bug,会不会出现错误。测试是整个程序的最后一步,这是一个艰巨的任务,是一个程序要发布出去,能不出现失误最重要的部分。目前我国的IT行业也是很缺测试方面的人才,这由此证明测试的重要性与必要性。在整个程序开发过程中,测试的重要性比亚于其他任何一个步骤。
6.1 测试的重要性
测试的重要性可以从其人才需求可以看出,目前的IT行业急需要成熟的软件测试员。其次我们可以看到,在每一个软件开发模型中,软件测试都是存在的。这也证明着它的重要性。它的存在可以就是为了让软件进入市场前确保程序的可运行性等。 6.2测试运行结果截图
测试用例1:用不存在的用户名直接登陆
预期测试结果:提示用户进行注册操作
测试结果如图6.2.1
图6.2.1没有用户的测试图
测试用例2:注册界面少些一个信息,直接保存
27
职场大变样社区(www.zcdby.com):下载毕业设计成品 预期测试结果:提示填写缺少的信息
测试结果如图6.2.2
图6.2.2缺少基本信息测试图 测试用例3:快速设定没有输入周期时间,直接开始 预期测试结果:提示填写时长
测试结果如图6.2.3
图6.2.3没有记录周期的测试 测试用例4:保存日记刷新日记列表测试。
预期测试:日记会出现在列表内。
测试结果如图6.2.4与6.2.5
28
职场大变样社区(www.zcdby.com):下载毕业设计成品
图6.2.4保存界面 图6.2.5 刷新后的日记列表 6.3系统评价
整个系统是比较简单且流畅的,没有什么技术上的大问题,也没有什么大的功能模块,就只是实现了我想要达到的模块内容。虽然功能比较简单,用的技术也比较简单,但对于初学者来讲,我觉得我已经达到了我需求,这是非常重要的一点。
在技术实现方面,整体还是比较欠缺,不成熟,我需要在技术层次方面多加练习以实现更好更强大的功能。程序从功能的实现方面还是不错的,没有大的问题,就是过于简单,就是对于比较厉害的人来说太过容易,而且整个程序的流畅度与反应时间也是比较缓慢,这是我需要加强的地方。也是以后我需要实现的地方。
29
职场大变样社区(www.zcdby.com):下载毕业设计成品
7 总结
7.1 遇到的问题
第一次接触android,所以要从头开始学习遇到了很多问题,虽然会出现很多障碍但是毕竟也大部分使用java语言开发,在这过程中只要有自己的思路就相对来说比较容易,接下来就是通过语言的实现。我主要遇到android的四大组件的理解和使用刚开始不知道如何用相关的组件。而且,android的组件相当的多对于我们初学者学习比较困难。而且,就光看安卓班的视频,都不曾真正的实践,没有有真正的弄懂它的使用方法,所以,我们在毕业设计中出现很多问题。 还有一个很遗憾的问题,那就是在整个过程中很多想实现的功能并不都能很快的实现,有些功能我到最后也没有实现,所以很可惜,但正是这样的遗憾,会使我更加向上,然后去努力的找到更好的方式去实现。一直想做一个日历的界面,但是最后也因为很多原因没有实现。
7.2 解决方法
由于android是用java语言编写的,自己学习java还比较可以,语言不是主要的问题就是android那四个组件的使用和理解,对于自己不理解的地方多亏指导老师的帮助,对于其一些接口查阅android的相关API接口文档,通过自己慢慢地实现一个个的功能理解和消化相关的知识,对于解决不了的问题,向老师和同学们求助,然后自己在一点点的理解。在这过程中我还是会去很多比较好的安卓论坛上去寻找解决方案,或者找到别人的代码进行参考,在论坛上会有很多的优秀的作品都有参考价值,所以很好。 7.3 收获体会
经过这么长时间的毕设时间,觉得特别充实,尤其是在这过程中,挑战了之前一直没有接触的安卓,这是我完成了自己的一个小小的心愿。虽然过程是辛苦的,在此次的毕设中还是存在很多不,这是一些不足,没有很好的完成自己想要的成果,但也是一次不小的进步,至少让我觉得安卓离我不是很远,也为自己以后能更好的学习安卓积攒了经验,这会让我有信心去继续努力。毕设的时间让我不断的充实自己,不断的进步,尤其是在这过程中,收到了老师跟同学的帮助也特别多,总之是在痛并快乐中完成了。
30
职场大变样社区(www.zcdby.com):下载毕业设计成品
尤其是在此次的过程中有些功能到最后也没有实现,所以特别的伤心,但是这会促进我使进步,在整个毕设过程中我发现,一定的能力加上一定的毅力是特别重要的,在遇到不会的时候还是不要着急,一定要沉着,这样才能冷静的处理任何事情。一切的成功都需要经过磨砺,收获成功的时候真的觉得一切的一切都是值得,哪怕是一个的幸福,那之后所付出的所有都是很值得的,我觉得这就是劳动所带来的快乐。
31
职场大变样社区(www.zcdby.com):下载毕业设计成品
主要参考文献(资料)
[1] 张秀香.基于Android的健康管理系统客户端的设计与实现[D].大连理工大学,2012.
[2] 王克峰.基于Android的信息推送管理系统的设计与实现[D].大连理工大学,2012.
[3] 邵翔,徐日.基于Android的事务管理型应用软件的设计与实现[J].北京电子科技学
院学报,2011, 19(4).
[4] 徐孝成.基于Android的密码管理系统的设计与实现[D].北京林业大学,2013.
[5] 李家科.Android系统分析与开发[D].兰州交通大学,2014.
[6] 吴想想.基于Android平台软件开发方法的研究与应用[D].北京邮电大学,2011.
[7] ContiJP.TheAndroidarecoming.Engineering& Technology,2008,3(9):153~156. [8] Haitham AL-Hajri,Krishnum Sasurooah.Iphone Forensic Methodoligy and
Tools[J].Proceedings if the 6th Australian Digital Forensic Conference,2008:4-18. 9] Yonghong Wu, Jianchao Luo, Lei Luo.Porting mobile web application engine to the [
Android platform.2010 10th IEEE International Conference on Computer and
Information Technology (CIT 2010),2010.
[10] Jerry Gao,Mansi Modak,Satyavathi Dornadula, et al.Mobile Jabber IM: A
Wireless-Based Text Chatting System,2004.
32
职场大变样社区(www.zcdby.com):下载毕业设计成品
致 谢
不知不觉,已经到了大学的尾声,觉得很遗憾,也觉得很感恩。遗憾的是我就要离开自己生活了四年的学校,觉得可惜跟不舍。感谢学校这四年的培养。首先我要感谢我的毕设老师,自己没接触过安卓就选了这个课题,很感谢他们一直不厌其烦的教导我,即使是很简单的东西也很细心的教导。也感谢他们对不知道如何下手写论文的我知道方向,使我的毕业设计能顺利的进行。
其次,就是在基地的老师们,这一年的时间谢谢你们的关照,谢谢你们的细心教导。这一年中跟着你们学了好很多,不管是学业还在社会面试中,你们都起到了很大作用, 在毕业设计中帮助我的同学们,这期间你们所给予的帮助是不能被忽视的。
我还要感谢我的舍友以及大学四年的同学~最最重要的是我们的导员。这四年的时光你们见证我从一个人性的小孩成长成为一个目前还算比较成熟的人。感谢这四年中你们对我支持跟不离不弃,跟我走过这四年中每个重要的或开心或伤心的时刻,真的舍不得~
最后我特别的感谢我大学的所有教过我的老师,从你们的身上学到的东西不止是知识,在整个大学生活中我成长的太快,也太多,不是一两句就能说完的,整个大学生活都过的很充实,所以特别感谢在这过程中帮助我成长的人们。
谢谢你们~
33
职场大变样社区(www.zcdby.com):下载毕业设计成品
外文文献
Thinking of Java Programming
Introduction to Objects
“We cut nature up, organize it into concepts, and ascribe significances as we do, largely because we are parties to an agreement that holds throughout our speech community and is codified in the patterns of our language … we cannot talk at all except by subscribing to the organization and classification of data which the agreement decrees.” Benjamin Lee Whorf (1897-1941)
The genesis of the computer revolution was in a machine. The genesis of our programming languages thus tends to look like that machine.
But computers are not so much machines as they are mind amplification tools (“bicycles for the mind,” as Steve Jobs is fond of saying) and a different kind of expressive medium. As a result, the tools are beginning to look less like machines and more like parts of our minds, and also like other forms of expression such as writing, painting, sculpture, animation, and filmmaking. Object-oriented programming (OOP) is part of this movement toward using the computer as an expressive medium.
This chapter will introduce you to the basic concepts of OOP, including an overview of development methods. This chapter, and this book, assumes that you have some programming experience, although not necessarily in C. If you think you need more preparation in programming before tackling this book, you should work through the Thinking
in C multimedia seminar, downloadable from www.MindView.net.
This chapter is background and supplementary material. Many people do not feel comfortable wading into object-oriented programming without understanding the big picture first. Thus, there are many concepts that are introduced here to give you a solid overview of OOP. However, other people may not get the big picture concepts until they’ve seen some of
the mechanics first; these people may become bogged down and lost without some code to get their hands on. If you’re part of this latter group and are eager to get to the specifics of the
34
职场大变样社区(www.zcdby.com):下载毕业设计成品
language, feel free to jump past this chapter—skipping it at this point will not prevent you
from writing programs or learning the language. However, you will want to come back here eventually to fill in your knowledge so you can understand why objects are important and how to design with them.
The progress of abstraction
All programming languages provide abstractions. It can be argued that the complexity of the problems you’re able to solve is directly related to the kind and quality of abstraction. By “kind” I mean, “What is it that you are abstracting?” Assembly language is a small
abstraction of the underlying machine. Many so-called “imperative” languages that followed
(such as FORTRAN, BASIC, and C) were abstractions of assembly language. These languages are big improvements over assembly language, but their primary abstraction still requires you to think in terms of the structure of the computer rather than the structure of the problem you are trying to solve. The programmer must establish the association between the machine model (in the “solution space,” which is the place where you’re implementing that solution, such as a computer) and the model of the problem that is actually being solved (in the 16 Thinking in Java Bruce Eckel
1 Some language designers have decided that object-oriented programming by itself is not adequate to easily solve all programming problems, and advocate the combination of various approaches into multiparadigm programming languages. See Multiparadigm
Programming in Leda by Timothy Budd (Addison-Wesley, 1995).
“problem space,” which is the place where the problem exists, such as a business). The effort required to perform this mapping, and the fact that it is extrinsic to the programming language, produces programs that are difficult to write and expensive to maintain, and as a side effect created the entire “programming methods” industry.
The alternative to modeling the machine is to model the problem you’re trying to solve. Early languages such as LISP and APL chose particular views of the world (“All problems
are ultimately lists” or “All problems are algorithmic,” respectively). Prolog casts all
35
职场大变样社区(www.zcdby.com):下载毕业设计成品
problems into chains of decisions. Languages have been created for constraint-based programming and for programming exclusively by manipulating graphical symbols. (The latter proved to be too restrictive.) Each of these approaches may be a good solution to the particular class of problem they’re designed to solve, but when you step outside of that domain they become awkward.
The object-oriented approach goes a step further by providing tools for the programmer to represent elements in the problem space. This representation is general enough that the programmer is not constrained to any particular type of problem. We refer to the elements in the problem space and their representations in the solution space as “objects.” (You will also need other objects that don’t have problem-space analogs.) The idea is that the program is
allowed to adapt itself to the lingo of the problem by adding new types of objects, so when you read the code describing the solution, you’re reading words that also express the problem. This is a more flexible and powerful language abstraction than what we’ve had before.1 Thus, OOP allows you to describe the problem in terms of the problem, rather than in terms of the computer where the solution will run. There’s still a connection back to the computer: Each object looks quite a bit like a little computer—it has a state, and it has operations that you can
ask it to perform. However, this doesn’t seem like such a bad analogy to objects in the real world—they all have characteristics and behaviors.
Alan Kay summarized five basic characteristics of Smalltalk, the first successful object-oriented language and one of the languages upon which Java is based. These characteristics represent a pure approach to object-oriented programming:
Everything is an object. Think of an object as a fancy variable; it stores data, but you can “make requests” to that object, asking it to perform operations on itself. In theory, you
can take any conceptual component in the problem you’re trying to solve (dogs, buildings, services, etc.) and represent it as an object in your program.
A program is a bunch of objects telling each other what to do by sending messages.
To make a request of an object, you “send a message” to that object. More concretely, you can think of a message as a request to call a method that belongs to a particular object.
Each object has its own memory made up of other objects. Put another way, you
create a new kind of object by making a package containing existing objects. Thus, you can
36
职场大变样社区(www.zcdby.com):下载毕业设计成品
build complexity into a program while hiding it behind the simplicity of objects.
Every object has a type. Using the parlance, each object is an instance of a class, in
which “class” is synonymous with “type.” The most important distinguishing characteristic of a class is “What messages can you send to it?”
All objects of a particular type can receive the same messages. This is actually a
loaded statement, as you will see later. Because an object of type “circle” is also an object of type “shape,” a circle is guaranteed to accept shape messages. This
Introduction to Objects 17
2 This is actually a bit restrictive, since objects can conceivably exist in different machines and address spaces, and they can also be stored on disk. In these cases, the identity of the object must be determined by something other than memory address.
3 Some people make a distinction, stating that type determines the interface while class is a particular implementation of that interface.
means you can write code that talks to shapes and automatically handle anything that fits the description of a shape. This substitutability is one of the powerful concepts in OOP.
Booch offers an even more succinct description of an object:
An object has state, behavior and identity.
This means that an object can have internal data (which gives it state), methods (to produce behavior), and each object can be uniquely distinguished from every other object—to put this in a concrete sense, each object has a unique address in memory.2
An object has an interface
Aristotle was probably the first to begin a careful study of the concept of type; he spoke
of “the class of fishes and the class of birds.” The idea that all objects, while being unique,
are also part of a class of objects that have characteristics and behaviors in common was used directly in the first object-oriented language, Simula-67, with its fundamental keyword class
that introduces a new type into a program.
Simula, as its name implies, was created for developing simulations such as the classic
37
职场大变样社区(www.zcdby.com):下载毕业设计成品
“bank teller problem.” In this, you have numerous tellers, customers, accounts, transactions, and units of money—a lot of “objects.” Objects that are identical except for their state during
a program’s execution are grouped together into “classes of objects,” and that’s where the keyword class came from. Creating abstract data types (classes) is a fundamental concept in object-oriented programming. Abstract data types work almost exactly like built-in types: You can create variables of a type (called objects or instances in object-oriented parlance)
and manipulate those variables (called sending messages or requests; you send a message
and the object figures out what to do with it). The members (elements) of each class share some commonality: Every account has a balance, every teller can accept a deposit, etc. At the same time, each member has its own state: Each account has a different balance, each teller has a name. Thus, the tellers, customers, accounts, transactions, etc., can each be represented with a unique entity in the computer program. This entity is the object, and each object belongs to a particular class that defines its characteristics and behaviors.
So, although what we really do in object-oriented programming is create new data types, virtually all object-oriented programming languages use the “class” keyword. When you see the word “type” think “class” and vice versa.3
Since a class describes a set of objects that have identical characteristics (data elements) and behaviors (functionality), a class is really a data type because a floating point number, for example, also has a set of characteristics and behaviors. The difference is that a programmer defines a class to fit a problem rather than being forced to use an existing data type that was designed to represent a unit of storage in a machine. You extend the programming language by adding new data types specific to your needs. The programming system welcomes the new classes and gives them all the care and type checking that it gives to built-in types.
The object-oriented approach is not limited to building simulations. Whether or not you agree that any program is a simulation of the system you’re designing, the use of OOP
techniques can easily reduce a large set of problems to a simple solution.
Once a class is established, you can make as many objects of that class as you like, and then manipulate those objects as if they are the elements that exist in the problem you are trying to solve. Indeed, one of the challenges of object-oriented programming is to create a one-to-one mapping between the elements in the problem space and objects in the solution
38
职场大变样社区(www.zcdby.com):下载毕业设计成品
space.
But how do you get an object to do useful work for you? There needs to be a way to make a request of the object so that it will do something, such as complete a transaction, draw something on the screen, or turn on a switch. And each object can satisfy only certain requests. The requests you can make of an object are defined by its interface, and the type is
what determines the interface. A simple example might be a representation of a light bulb:
Light lt = new Light();
lt.on();
The interface determines the requests that you can make for a particular object. However, there must be code somewhere to satisfy that request. This, along with the hidden data, comprises the implementation. From a procedural programming standpoint, it’s not that
complicated. A type has a method associated with each possible request, and when you make a particular request to an object, that method is called. This process is usually summarized by saying that you “send a message” (make a request) to an object, and the object figures out what to do with that message (it executes code).
Here, the name of the type/class is Light, the name of this particular Light object is lt,
and the requests that you can make of a Light object are to turn it on, turn it off, make it
brighter, or make it dimmer. You create a Light object by defining a “reference” (lt) for that
object and calling new to request a new object of that type. To send a message to the object, you state the name of the object and connect it to the message request with a period (dot). From the standpoint of the user of a predefined class, that’s pretty much all there is to programming with objects.
The preceding diagram follows the format of the Unified Modeling Language (UML).
Each class is represented by a box, with the type name in the top portion of the box, any data
members that you care to describe in the middle portion of the box, and the methods (the
functions that belong to this object, which receive any messages you send to that object) in the bottom portion of the box. Often, only the name of the class and the public methods are shown in UML design diagrams, so the middle portion is not shown, as in this case. If you’re interested only in the class name, then the bottom portion doesn’t need to be shown, either.
39
职场大变样社区(www.zcdby.com):下载毕业设计成品
An object provides services
While you’re trying to develop or understand a program design, one of the best ways to think about objects is as “service providers.” Your program itself will provide services to the user, and it will accomplish this by using the services offered by other objects. Your goal is to 18 Thinking in Java Bruce EckelIntroduction to Objects 19
4 I’m indebted to my friend Scott Meyers for this term.
produce (or even better, locate in existing code libraries) a set of objects that provide the ideal services to solve your problem.
A way to start doing this is to ask, “If I could magically pull them out of a hat, what objects would solve my problem right away?” For example, suppose you are creating a bookkeeping program. You might imagine some objects that contain pre-defined bookkeeping input screens, another set of objects that perform bookkeeping calculations, and an object that handles printing of checks and invoices on all different kinds of printers. Maybe some of these objects already exist, and for the ones that don’t, what would they look
like? What services would those objects provide, and what objects would they need to fulfill their obligations? If you keep doing this, you will eventually reach a point where you can say either, “That object seems simple enough to sit down and write” or “I’m sure that object must exist already.” This is a reasonable way to decompose a problem into a set of objects.
Thinking of an object as a service provider has an additional benefit: It helps to improve the cohesiveness of the object. High cohesion is a fundamental quality of software design: It means that the various aspects of a software component (such as an object, although this could also apply to a method or a library of objects) “fit together” well. One problem people
have when designing objects is cramming too much functionality into one object. For example, in your check printing module, you may decide you need an object that knows all about formatting and printing. You’ll probably discover that this is too much for one object,
and that what you need is three or more objects. One object might be a catalog of all the possible check layouts, which can be queried for information about how to print a check. One object or set of objects can be a generic printing interface that knows all about different kinds of printers (but nothing about bookkeeping—this one is a candidate for buying rather than
40
职场大变样社区(www.zcdby.com):下载毕业设计成品
writing yourself). And a third object could use the services of the other two to accomplish the task. Thus, each object has a cohesive set of services it offers. In a good object-oriented design, each object does one thing well, but doesn’t try to do too much. This not only allows the discovery of objects that might be purchased (the printer interface object), but it also produces new objects that might be reused somewhere else (the catalog of check layouts).
Treating objects as service providers is a great simplifying tool. This is useful not only during the design process, but also when someone else is trying to understand your code or reuse an object. If they can see the value of the object based on what service it provides, it makes it much easier to fit it into the design.
The hidden implementation
It is helpful to break up the playing field into class creators (those who create new data
types) and client programmers4 (the class consumers who use the data types in their
applications). The goal of the client programmer is to collect a toolbox full of classes to use for rapid application development. The goal of the class creator is to build a class that exposes only what’s necessary to the client programmer and keeps everything else hidden. Why? Because if it’s hidden, the client programmer can’t access it, which means that the class creator can change the hidden portion at will without worrying about the impact on anyone else. The hidden portion usually represents the tender insides of an object that could easily be corrupted by a careless or uninformed client programmer, so hiding the implementation reduces program bugs.
In any relationship it’s important to have boundaries that are respected by all parties involved. When you create a library, you establish a relationship with the client programmer, who is also a programmer, but one who is putting together an application by using your library, possibly to build a bigger library. If all the members of a class are available to everyone, then the client programmer can do anything with that class and there’s no way to enforce rules. Even though you might really prefer that the client programmer not directly
manipulate some of the members of your class, without access control there’s no way to prevent it. Everything’s naked to the world.
So the first reason for access control is to keep client programmers’ hands off portions
41
职场大变样社区(www.zcdby.com):下载毕业设计成品
they shouldn’t touch—parts that are necessary for the internal operation of the data type but not part of the interface that users need in order to solve their particular problems. This is actually a service to client programmers because they can easily see what’s important to them
and what they can ignore.
The second reason for access control is to allow the library designer to change the internal workings of the class without worrying about how it will affect the client programmer. For example, you might implement a particular class in a simple fashion to ease development, and then later discover that you need to rewrite it in order to make it run faster. If the interface and implementation are clearly separated and protected, you can accomplish this easily.
Java uses three explicit keywords to set the boundaries in a class: public, private, and
protected. These access specifiers determine who can use the definitions that follow. public
means the following element is available to everyone. The private keyword, on the other
hand, means that no one can access that element except you, the creator of the type, inside methods of that type. private is a brick wall between you and the client programmer.
Someone who tries to access a private member will get a compile-time error. The protected
keyword acts like private, with the exception that an inheriting class has access to protected
members, but not private members. Inheritance will be introduced shortly.
Java also has a “default” access, which comes into play if you don’t use one of the aforementioned specifiers. This is usually called package access because classes can access
the members of other classes in the same package (library component), but outside of the
package those same members appear to be private.
Reusing the implementation
Once a class has been created and tested, it should (ideally) represent a useful unit
of code. It turns out that this reusability is not nearly so easy to achieve as many would
hope; it takes experience and insight to produce a reusable object design. But once you
have such a design, it begs to be reused. Code reuse is one of the greatest advantages that
object-oriented programming languages provide.
42
职场大变样社区(www.zcdby.com):下载毕业设计成品
The simplest way to reuse a class is to just use an object of that class directly, but you can also place an object of that class inside a new class. We call this “creating a member object.” Your new class can be made up of any number and type of other objects, in any combination that you need to achieve the functionality desired in your new class. Because you are composing a new class from existing classes, this concept is called composition (if the composition happens dynamically, it’s usually called aggregation).
Composition is often referred to as a “has-a” relationship, as in “A car has an engine.”
43
职场大变样社区(www.zcdby.com):下载毕业设计成品
中文翻译
Java编程思想
第1 章 对象入门
“为什么面向对象的编程会在软件开发领域造成如此震憾的影响,”面向对象编程(OOP)具有多方面的吸引力。对管理人员,它实现了更快和更廉价的开发与维护过程。对分析与设计人员,建模处理变得更加简单,能生成清晰、易于维护的设计方案。对程序员,对象模型显得如此高雅和浅显。此外,面向对象工具以及库的巨大威力使编程成为一项更使人愉悦的任务。每个人都可从中获益,至少表面如此。如果说它有缺点,那就是掌握它需付出的代价。思考对象的时候,需要采用形象思维,而不是程序化的思维。与程序化设计相比,对象的设计过程更具挑战性——特别是在尝试创建可重复使用(可再生)的对象时。过去,那些初涉面向对象编程领域的人都必须进行一项令人痛苦的选择:
(1) 选择一种诸如Smalltalk 的语言,“出师”前必须掌握一个巨型的库。
(2) 选择几乎根本没有库的C++(注释?),然后深入学习这种语言,直至能自行编写对象库。
?:幸运的是,这一情况已有明显改观。现在有第三方库以及标准的C++库供选用。
事实上,很难很好地设计出对象——从而很难设计好任何东西。因此,只有数量相当少的“专家”能设计出最好的对象,然后让其他人享用。对于成功的OOP 语言,它们不仅集成了这种语言的语法以及一个编译程序(编译器),而且还有一个成功的开发环境,其中包含设计优良、易于使用的库。所以,大多数程序员的首
要任务就是用现有的对象解决自己的应用问题。本章的目标就是向大家揭示出面向对象编程的概念,并证明它有多么简单。
本章将向大家解释Java 的多项设计思想,并从概念上解释面向对象的程序设计。但要注意在阅读完本章后,并不能立即编写出全功能的Java 程序。所有详细的说明和示例会在本书的其他章节慢慢道来。
1.1 抽象的进步
44
职场大变样社区(www.zcdby.com):下载毕业设计成品
所有编程语言的最终目的都是提供一种“抽象”方法。一种较有争议的说法是:解决问题的复杂程度直接取决于抽象的种类及质量。这儿的“种类”是指准备对什么进行“抽象”,汇编语言是对基础机器的少量抽象。后来的许多“命令式”语言(如FORTRAN,BASIC 和C)是对汇编语言的一种抽象。与汇编语言相比,这些语言已有了长足的进步,但它们的抽象原理依然要求我们着重考虑计算机的结构,而非考虑问题本身的结构。在机器模型(位于“方案空间”)与实际解决的问题模型(位于“问题空间”)之间,程序员必须建立起一种联系。这个过程要求人们付出较大的精力,而且由于它脱离了编程语言本身的范围,造成程序代码很难编写,而且要花较大的代价进行维护。由此造成的副作用便是一门完善的“编程方法”学科。
为机器建模的另一个方法是为要解决的问题制作模型。对一些早期语言来说,如LISP 和APL,它们的做法是“从不同的角度观察世界”——“所有问题都归纳为列表”或“所有问题都归纳为算法”。PROLOG 则将所有问题都归纳为决策链。对于这些语言,我们认为它们一部分是面向基于“强制”的编程,另一部分则是专为处理图形符号设计的。每种方法都有自己特殊的用途,适合解决某一类的问题。但只要超出了它们力所能及的范围,就会显得非常笨拙。
面向对象的程序设计在此基础上则跨出了一大步,程序员可利用一些工具表达问题空间内的元素。由于这种表达非常普遍,所以不必受限于特定类型的问题。我们将问题空间中的元素以及它们在方案空间的表示物称作“对象”(Object)。当然,还有一些在问题空间没有对应体的其他对象。通过添加新的对象类型,程序可进行灵活的调整,以便与特定的问题配合。所以在阅读方案的描述代码时,会读到对问题进行表达的话
语。与我们以前见过的相比,这无疑是一种更加灵活、更加强大的语言抽象方法。总之,OOP 允许我们根据问题来描述问题,而不是根据方案。然而,仍有一个联系途径回到计算机。每个对象都类似一台小计算机;
它们有自己的状态,而且可要求它们进行特定的操作。与现实世界的“对象”或者“物体”相比,编程“对象”与它们也存在共通的地方:它们都有自己的特征和行为。
Alan Kay 总结了Smalltalk 的五大基本特征。这是第一种成功的面向对象程序设计语言,也是Java 的基础语言。通过这些特征,我们可理解“纯粹”的面向对象程序设计方法是什么样的:
(1) 所有东西都是对象。可将对象想象成一种新型变量;它保存着数据,但可要求它对自身进行操作。理论上讲,可从要解决的问题身上提出所有概念性的组件,然后在
45
职场大变样社区(www.zcdby.com):下载毕业设计成品
程序中将其表达为一个对象。
(2) 程序是一大堆对象的组合;通过消息传递,各对象知道自己该做些什么。为了向对象发出请求,需向那个对象“发送一条消息”。更具体地讲,可将消息想象为一个调用请求,它调用的是从属于目标对象的一个子例程或函数。
(3) 每个对象都有自己的存储空间,可容纳其他对象。或者说,通过封装现有对象,可制作出新型对象。所以,尽管对象的概念非常简单,但在程序中却可达到任意高的复杂程度。
(4) 每个对象都有一种类型。根据语法,每个对象都是某个“类”的一个“实例”。其中,“类”(Class)是“类型”(Type)的同义词。一个类最重要的特征就是“能将什么消息发给它,”。
(5) 同一类所有对象都能接收相同的消息。这实际是别有含义的一种说法,大家不久便能理解。由于类型为“圆”(Circle)的一个对象也属于类型为“形状”(Shape)的一个对象,所以一个圆完全能接收形状消息。这意味着可让程序代码统一指挥“形状”,令其自动控制所有符合“形状”描述的对象,其中自然包括“圆”。这一特性称为对象的“可替换性”,是OOP 最重要的概念之一。
一些语言设计者认为面向对象的程序设计本身并不足以方便解决所有形式的程序问题,提倡将不同的方法组合成“多形程序设计语言”(注释?)。
?:参见Timothy Budd 编著的《Multiparadigm Programming in Leda》,Addison-Wesley 1995 年出版。
1.2 对象的接口
亚里士多德或许是认真研究“类型”概念的第一人,他曾谈及“鱼类和鸟类”的问题。在世界首例面向对象语言Simula-67 中,第一次用到了这样的一个概念:
所有对象——尽管各有特色——都属于某一系列对象的一部分,这些对象具有通用的特征和行为。在Simula-67 中,首次用到了class 这个关键字,它为程序引入了一个全新的类型(clas 和type 通常可互换使用;注释?)。
?:有些人进行了进一步的区分,他们强调“类型”决定了接口,而“类”是那个接口的一种特殊实现方式。
Simula 是一个很好的例子。正如这个名字所暗示的,它的作用是“模拟”(Simulate)
46
职场大变样社区(www.zcdby.com):下载毕业设计成品
象“银行出纳员”这样的经典问题。在这个例子里,我们有一系列出纳员、客户、帐号以及交易等。每类成员(元素)都具有一些通用的特征:每个帐号都有一定的余额;每名出纳都能接收客户的存款;等等。与此同时,每个成员都有自己的状态;每个帐号都有不同的余额;每名出纳都有一个名字。所以在计算机程序中,能用独一无二的实体分别表示出纳员、客户、帐号以及交易。这个实体便是“对象”,而且每个对象都隶属一个特定的“类”,那个类具有自己的通用特征与行为。因此,在面向对象的程序设计中,尽管我们真正要做的是新建各种各样的数据“类型”(Type),但几乎所有面向对象的程序设计语言都采用了“class”关键字。当您看到“type”这个字的时候,请同时想到“class”;反之亦然。建好一个类后,可根据情况生成许多对象。随后,可将那些对象作为要解决问题中存在的元素进行处理。事实上,当我们进行面向对象的程序设计时,面临的最大一项挑战性就是:如何在“问题空间”(问题实际存在的地方)的元素与“方案空间”(对实际问题进行建模的地方,如计算机)的元素之间建立理想的“一对一”对应或映射关系。如何利用对象完成真正有用的工作呢,必须有一种办法能向对象发出请求,令其做一些实际的事情,比如完成一次交易、在屏幕上画一些东西或者打开一个开关等等。每个对象仅能接受特定的请求。我们向对象发出的请求是通过它的“接口”(Interface)定义的,对象的“类型”或“类”则规定了它的接口形式。“类
型”与“接口”的等价或对应关系是面向对象程序设计的基础。
下面让我们以电灯泡为例:
Light lt = new Light();
lt.on();
在这个例子中,类型,类的名称是Light,可向Light 对象发出的请求包括包括打开(on)、关闭(off)、变得更明亮(brighten )或者变得更暗淡(dim)。通过简单地声明一个名字(lt),我们为Light 对象创建了一个“句柄”。然后用new 关键字新建类型为Light 的一个对象。再用等号将其赋给句柄。为了向对象发送一条消息,我们列出句柄名(lt),再用一个句点符号(.)把它同消息名称(on)连接起来。从中可以看出,使用一些预先定义好的类时,我们在程序里采用的代码是非常简单和直观的。 1.3 实现方案的隐藏
为方便后面的讨论,让我们先对这一领域的从业人员作一下分类。从根本上说,大
47
职场大变样社区(www.zcdby.com):下载毕业设计成品
致有两方面的人员涉足面向对象的编程:“类创建者”(创建新数据类型的人)以及“客户程序员”(在自己的应用程序中采用现成数据类型的人;注释?)。对客户程序员来讲,最主要的目标就是收集一个充斥着各种类的编程“工具箱”,以便快速开发符合自己要求的应用。而对类创建者来说,他们的目标则是从头构建一个类,只向客户
程序员开放有必要开放的东西(接口),其他所有细节都隐藏起来。为什么要这样做,隐藏之后,客户程序员就不能接触和改变那些细节,所以原创者不用担心自己的作品会受到非法修改,可确保它们不会对其他人造成影响。
?:感谢我的朋友Scott Meyers,是他帮我起了这个名字。
“接口”(Interface)规定了可对一个特定的对象发出哪些请求。然而,必须在某个地方存在着一些代码,以便满足这些请求。这些代码与那些隐藏起来的数据便叫作“隐藏的实现”。站在程式化程序编写(Procedural Programming )的角度,整个问题并不显得复杂。一种类型含有与每种可能的请求关联起来的函数。一旦向对象发出一个特定的请求,就会调用那个函数。我们通常将这个过程总结为向对象“发送一条
消息”(提出一个请求)。对象的职责就是决定如何对这条消息作出反应(执行相应的代码)。
对于任何关系,重要一点是让牵连到的所有成员都遵守相同的规则。创建一个库时,相当于同客户程序员建立了一种关系。对方也是程序员,但他们的目标是组合出一个特定的应用(程序),或者用您的库构建一个更大的库。若任何人都能使用一个类的所有成员,那么客户程序员可对那个类做任何事情,没有办法强制他们遵守任何约束。即便非常不愿客户程序员直接操作类内包含的一些成员,但倘若未进行访问控制,就没有办法阻止这一情况的发生——所有东西都会暴露无遗。有两方面的原因促使我们控制对成员的访问。第一个原因是防止程序员接触他们不该接触的东西——通常是内部数据类型的设计思想。若只是为了解决特定的问题,用户只需操作接口即可,毋需明白这些信息。我们向用户提供的实际是一种服务,因为他们很容易就可看出哪些对自己非常重要,以及哪些可忽略不计。进行访问控制的第二个原因是允许库设计人员修改内部结构,不用担心它会对客户程序员造成什么影响。例如,我们最开始可能设计了一个形式简单的类,以便简化开发。以后又决定进行改写,使其更快地运行。若接口与实现方法早已隔离开,并分别受到保护,就可放心做到这一点,只要求用户重新链接一下即可。
Java 采用三个显式(明确)关键字以及一个隐式(暗示)关键字来设置类边界:public,private,protected 以及暗示性的friendly。若未明确指定其他关键字,则默认为
48
职场大变样社区(www.zcdby.com):下载毕业设计成品
后者。这些关键字的使用和含义都是相当直观的,它们决定了谁能使用后续的定义内容。“public”(公共)意味着后续的定义任何人均可使用。而在另一方面,“private”(私有)意味着除您自己、类型的创建者以及那个类型的内部函数成员,其他任何人都不能访问后续的定义信息。private 在您与客户程序员之间竖起了一堵墙。若有人试图访问私有成员,就会得到一个编译期错误。“friendly ”(友好的)涉及“包装”或“封装”(Package)的概念——即Java 用来构建库的方法。若某样东西是“友好的”,意味着它只能在这个包装的范围内使用(所以这一访问级别有时也叫作“包装访问”)。“protected”(受保护的)与“private”相似,只是一个继承的类可访问受保护的成员,但不能访问私有成员。继承的问题不久就要谈到。
1.4 方案的重复使用
创建并测试好一个类后,它应(从理想的角度)代表一个有用的代码单位。但并不象许多人希望的那样,这种重复使用的能力并不容易实现;它要求较多的经验以及洞察力,这样才能设计出一个好的方案,才有可能重复使用。
许多人认为代码或设计方案的重复使用是面向对象的程序设计提供的最伟大的一种杠杆。为重复使用一个类,最简单的办法是仅直接使用那个类的对象。但同时也能将那个类的一个对象置入一个新类。我们把这叫作“创建一个成员对象”。新类可由任意数量和类型的其他对象构成。无论如何,只要新类达到了设计要求即可。这个概念叫作“组织”——在现有类的基础上组织一个新类。有时,我们也将组织称作“包含”关系,比如“一辆车包含了一个变速箱”。
对象的组织具有极大的灵活性。新类的“成员对象”通常设为“私有”(Private),使用这个类的客户程序员不能访问它们。这样一来,我们可在不干扰客户代码的前提下,从容地修改那些成员。也可以在“运行期”更改成员,这进一步增大了灵活性。后面要讲到的“继承”并不具备这种灵活性,因为编译器必须对通过继承创建的类加以限制。
由于继承的重要性,所以在面向对象的程序设计中,它经常被重点强调。作为新加入这一领域的程序员,或许早已先入为主地认为“继承应当随处可见”。沿这种思路产生的设计将是非常笨拙的,会大大增加程序的复杂程度。相反,新建类的时候,首先应考虑“组织”对象;这样做显得更加简单和灵活。利用对象的组织,我们的设计可保持清爽。一旦需要用到继承,就会明显意识到这一点。
49
职场大变样社区(www.zcdby.com):下载毕业设计成品
1.5 继承:重新使用接口
就其本身来说,对象的概念可为我们带来极大的便利。它在概念上允许我们将各式各样数据和功能封装到一起。这样便可恰当表达“问题空间”的概念,不用刻意遵照基础机器的表达方式。在程序设计语言中,这些概念则反映为具体的数据类型(使用class 关键字)。
我们费尽心思做出一种数据类型后,假如不得不又新建一种类型,令其实现大致相同的功能,那会是一件非常令人灰心的事情。但若能利用现成的数据类型,对其进行“克隆”,再根据情况进行添加和修改,情况就显得理想多了。“继承”正是针对这个目标而设计的。但继承并不完全等价于克隆。在继承过程中,若原始类(正式名称叫作基础类、超类或父类)发生了变化,修改过的“克隆”类(正式名称叫作继承类或者子类)也会反映出这种变化。在Java 语言中,继承是通过extends 关键字实现的使用继承时,相当于创建了一个新类。这个新类不仅包含了现有类型的所有成员(尽管private 成员被隐藏起来,且不能访问),但更重要的是,它复制了基础类的接口。也就是说,可向基础类的对象发送的所有消息亦可原样发给衍生类的对象。根据可以发送的消息,我们能知道类的类型。这意味着衍生类具有与基础类相同的类型~为真正理解面向对象程序设计的含义,首先必须认识到这种类型的等价关系。
由于基础类和衍生类具有相同的接口,所以那个接口必须进行特殊的设计。也就是说,对象接收到一条特定的消息后,必须有一个“方法”能够执行。若只是简单地继承一个类,并不做其他任何事情,来自基础类接口的方法就会直接照搬到衍生类。这意味着衍生类的对象不仅有相同的类型,也有同样的行为,这一后果通常是我们不愿见到的。
有两种做法可将新得的衍生类与原来的基础类区分开。第一种做法十分简单:为衍生类添加新函数(功能)。这些新函数并非基础类接口的一部分。进行这种处理时,一般都是意识到基础类不能满足我们的要求,所以需要添加更多的函数。这是一种最简单、最基本的继承用法,大多数时候都可完美地解决我们的问题。然而,事先还是要仔细调查自己的基础类是否真的需要这些额外的函数。
1.5.1 改善基础类
尽管extends 关键字暗示着我们要为接口“扩展”新功能,但实情并非肯定如此。
50
职场大变样社区(www.zcdby.com):下载毕业设计成品
为区分我们的新类,第二个办法是改变基础类一个现有函数的行为。我们将其称作“改善”那个函数。为改善一个函数,只需为衍生类的函数建立一个新定义即可。我们的目标是:“尽管使用的函数接口未变,但它的新版本具有不同的表现”。
1.5.2 等价与类似关系
针对继承可能会产生这样的一个争论:继承只能改善原基础类的函数吗,若答案是肯定的,则衍生类型就是与基础类完全相同的类型,因为都拥有完全相同的接口。这样造成的结果就是:我们完全能够将衍生类的一个对象换成基础类的一个对象~可将其想象成一种“纯替换”。在某种意义上,这是进行继承的一种理想方式。此时,我们通常认为基础类和衍生类之间存在一种“等价”关系——因为我们可以理直气壮地说:“圆
就是一种几何形状”。为了对继承进行测试,一个办法就是看看自己是否能把它们套入这种“等价”关系中,看看是否有意义。但在许多时候,我们必须为衍生类型加入新的接口元素。所以不仅扩展了接口,也创建了一种新类型。这种新类型仍可替换成基础类型,但这种替换并不是完美的,因为不可在基础类里访问新函数。我们将其称作“类似”关系;新类型拥有旧类型的接口,但也包含了其他函数,所以不能说它们是完全等价的。举个例子来说,让我们考虑一下制冷机的情况。假定我们的房间连好了用于制冷的各种控制器;也就是说,我们已拥有必要的“接口”来控制制冷。现在假设机器出了故障,我们把它换成一台新型的冷、热两用空调,冬天和夏天均可使用。冷、热空调“类似”制冷机,但能做更多的事情。由于我们的房间只安装了控制制冷的设备,所以它们只限于同新机器的制冷部分打交道。新机器的接口已得到了扩展,但现有的系统并不知道除原始接口以外的任何东西。认识了等价与类似的区别后,再进行替换时就会有把握得多。尽管大多数时候“纯替换”已经足够,但您会发现在某些情况下,仍然有明显的理由需要在衍生类的基础上增添新功能。通过前面对这两种情况的讨论,相信大家已心中有数该如何做。
51