新版类和对象培训课件.ppt
- 【下载声明】
1. 本站全部试题类文档,若标题没写含答案,则无答案;标题注明含答案的文档,主观题也可能无答案。请谨慎下单,一旦售出,不予退换。
2. 本站全部PPT文档均不含视频和音频,PPT中出现的音频或视频标识(或文字)仅表示流程,实际无音频或视频文件。请谨慎下单,一旦售出,不予退换。
3. 本页资料《新版类和对象培训课件.ppt》由用户(晟晟文业)主动上传,其收益全归该用户。163文库仅提供信息存储空间,仅对该用户上传内容的表现方式做保护处理,对上传内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知163文库(点击联系客服),我们立即给予删除!
4. 请根据预览情况,自愿下载本文。本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
5. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007及以上版本和PDF阅读器,压缩文件请下载最新的WinRAR软件解压。
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 新版 对象 培训 课件
- 资源描述:
-
1、类和对象Class and Object精选关于对象式语言精选本课程并不系统讲授Eiffel语言但应学会“欣赏”Eiffel 语言(比较“纯”)它首先是一个对象式程序设计语言但强调从分析到设计到实现的平滑过渡2摘要精选三个世界:面向对象的建模面向对象原则类:对象程序静态的结构对象:运行时刻的结构对象创建:从类到对象对象引用:对象串起来3摘要三个世界三个世界:面向对象的建模面向对象的建模面向对象原则类:对象程序静态的结构对象:运行时刻的结构对象创建:从类到对象对象引用:对象串起来精选4三个世界精选客观世界 DVD播放机What is reality?复杂性 认识的主观性 问题世界抽象!A mod
2、el of a subset of the reality DVD播放机计算机(软件)世界A model of a model of a subset of the reality52022-12-12Institute of Computer SoftwareNanjing University现实世界现实世界问题世界问题世界软件世界软件世界Reality抽象抽象精选6摘要精选三个世界:面向对象的建模面向对象原则面向对象原则类:对象程序静态的结构对象:运行时刻的结构对象创建:从类到对象对象引用:对象串起来7面向对象原则精选Object OrientationEncapsulationAbst
3、ractionPolymorphismInheritance8面向对象原则精选Object OrientationEncapsulationAbstractionPolymorphismInheritance9回顾:抽象数据类型精选抽象数据类型(ADT):用数学方法定义对象集合和运算集合,仅通过运算的性质刻画数据对象,而独立于计算机中可能的表示方法ADT规约方法代数规范语法部分,公理部分ADT实现E1,E2,E310面向对象原则精选Object OrientationEncapsulationAbstractionPolymorphismInheritance11封装精选封装是把过程和数据包围
4、起来,对数据的访问只能通过已定义的界面。DVD播放机12精选封装lProfessor Clark needs to be able to teach four classes in the next semester.SubmitFinalGrades()AcceptCourseOffering()TakeSabbatical()Professor ClarkSetMaxLoad()Name:J ClarkEmployee ID:567138HireDate:07/25/1991Status:TenuredDiscipline:FinanceMaxLoad:4SetMaxLoad(4)13精选
5、封装信息/实现隐藏Deposit()Withdraw()Transfer()BalanceinsterestYTDOwnerAccount_number Deposit()Withdraw()Transfer()14精选封装封装使得对象在抽象所界定的范围内保持相对的独立,从而保证对象设计和对象实施方法的可靠分离。设计者和使用者分离!封装可以防止由于程序模块之间的互相依赖性而导致程序“不稳定”不会因某个部分的修改而导致整个系统许多部分都发生改动。封装是相对的!不能也没有必要阻止编程人员去查看实施细节。15精选封装 vs.继承矛盾吗?No!封装性主要指的是对象的封装性,引入继承机制后,对象仍然是封
6、装地很好的实体,其它对象与它通信只能发送消息。相似性:共享代码!继承:静态共享代码封装:动态共享代码16精选模块化传统的模块划分方法:要求组成的子程序(模块)有实际意义和明确的某个功能功能分解和结构分解OO的模块划分目标:生成一组有助于降低系统开发费用的模块,通过模块划分所得到的模块应该能够保证可以独立的生成和修改,每个模块的结构应该足够简单17精选模块化与抽象和封装密切相关模块的划分是以抽象出来的对象的性质和功能为依据的模块是封装得以实现的技术保障Booch:“模块化是反映一个被分解成一组相关联的松散结合模块组成系统的一个特性。”模块化以模块为基础18精选模块与类型的统一模块是软件分解的单元
7、,是语法概念类型是某些动态对象的静态描述,是语义概念传统语言 模块与类型分离对象语言 模块与类型统一类型:类是抽象数据类型的实现模块:类是对象式程序的基本组成单元基于类的面向对象的语言机制的强有力之处在于“类”统一了类型和模块19面向对象原则精选Object OrientationEncapsulationAbstractionPolymorphismInheritance20精选层次性封装性帮助隐藏细节;模块化使结构更加有序,但仍然不够!层次性是对抽象的排序和定位类结构关系(“is a”)对象结构关系(“part of”)实现方式继承:子类,父类单继承,多继承聚合:拥有关系/组合关系21精选
8、继承单继承CheckingSavingsSuperclass(parent)SubclassesInheritance RelationshipAncestorDescendentsAccount-balance-name-number+withdraw()+createStatement()22精选继承多继承Use multiple inheritance only when needed and always with caution!FlyingThingAnimalHorseWolfBirdHelicopterAirplaneMultiple Inheritance23精选继承继承 作
9、为模块扩展机制,体现开闭原则开闭原则,较好地支持软件的复用。继承 作为类型特化机制,体现分类方法,更合理地支持子类型实现。继承也是多态和动态绑定机制的基础。24精选子类对父类的扩展与特化继承表达的是一种分类演绎的认识方法“To program is to understand.”特化:包含范围变小,行为特殊化扩展:增加新的操作重定义:改变行为25精选The Open-Closed Principle(OCP)动机:软件是变化的!开闭原则:“Closed for Modification;Open for Extension”对修改关闭,对扩展开放!Why OCP?-重用How OCP?-继承2
10、6The Open-Closed PrincipleModules should be open and closed.Definitions:Open module:May be extended.Closed module:Usable by clients.May be approved,baselined and(if program unit)compiled.The rationales are complementary:For closing a module(managers perspective):Clients need it now.For keeping modul
11、es open(developers perspective):One frequently overlooks aspects of the problem.精选27The Open-Closed Principle(1)精选ACEDB28The Open-Closed Principle(2)精选ACEDBFAGH I29The Open-Closed Principle(3)精选ACEDBFAGH I30精选Example31面向对象原则精选Object OrientationEncapsulationAbstractionPolymorphismInheritance32精选多态多态性
12、使指相同的操作或函数、过程可作用于多种类型的对象上并获得不同的结果。不同的对象,收到同一消息可以产生不同的结果,这种现象称为多态性。多态性允许每个对象以适合自身的方式去响应共同的消息多态性增强了软件的灵活性和重用性。33精选多态ShapeCircleTriangleSquaredraw()draw()draw()draw()public class Shapes public static Shape randShape()switch(int)(Math.random()*3)default:case 0:return new Circle();case 1:return new Squar
13、e();case 2:return new Triangle();public static void main(String args)Shape s=new Shape3;/向数组里添加类型 for(int i=0;i s.length;i+)si=randShape();/用多态的方法调用 for(int i=0;i=0end精选40类的构成类名继承关系成员术语分类精选41Abstract data type POINTx:POINT REALy:POINT REAL:POINT REAL:POINT REALClass POINT:Choose a representation(pol
14、ar,cartesian)In polar representation,and are attributes,x and y are routines.精选yx42A simple classclass POINT featurex,y:REAL-Point cartesian coordinatesmove(a,b:REAL)is-Move by a horizontally and by b vertically.dox:=x+ay:=y+bendscale(factor:REAL)is-Scale by factor.dox:=factor*xy:=factor*yend精选43Cla
15、ss POINT(contd)distance(p:POINT):REAL is-Distance to pdoResult:=sqrt(x p.x)2+(y p.y)2)endro:REAL is-Distance to origin(0,0)doResult:=sqrt(x2+y2)endtheta:REAL is-Angle to horizontal axisdoendend精选44术语A class is an implementation of an abstract data type.Instances of the class may be created at run-ti
16、me;they are objects.类的实例在运行时刻产生,他们是对象。Every object is an instance of a class.每个对象都是一个类的实例。Note:In a pure O-O language such as Eiffel and Smalltalk this is true even of basic objects such as integers etc.Not true in C+or Java where such values have special status.精选45术语A class is characterized by fea
17、tures.Features comprise attributes(representing data fields of instances of the class)and routines(operations on instances).Routines are subdivided into procedures(effect on the instance,no result)and functions(result,normally no effect).Every operation(routine or attribute call)is relative to a dis
18、tinguished object,the current instance of the class.精选46Feature categories by role精选CommandQueryFeatureProcedureAttributeFunctionNo resultReturns resultComputationMemory47Feature categories by implementation精选ProcedureAttributeFunctionRoutineReturns resultNo resultMemoryComputationFeature48Feature c
19、ategories精选CommandQueryFeatureProcedureAttributeFunctionNo resultReturns resultComputationMemoryRoutineReturns resultNo resultMemoryComputationFeature49Alternative terminologyAttributes are also called instance variables or data member.Routines are also called methods,subprograms,or subroutines.Feat
20、ure call applying a certain feature of a class to an instance of that class is also called passing a message to that object.The notion of feature is particularly important as it provides a single term to cover both attributes and routines.It is often desirable not to specify whether a feature is an
21、attribute or a routine as expressed by the Uniform Access principle.精选50回顾:Uniform Access 访问一致性原则客户应能以实现无关的方式访问模块的功能,不管这个功能是通过存储还是计算来实现的。Facilities managed by a module must be accessible to clients in the same way whether implemented by computation or storage.精选51Uniform Accessbalance=list_of_deposi
22、ts.total list_of_withdrawals.total精选list_of_depositslist_of_withdrawalsbalancelist_of_depositslist_of_withdrawals(A2)(A1)52Uniform access through feature callTo access a property of a point p1,the notation is the same regardless of the representation,e.g.p1.xwhich is applicable both in cartesian rep
23、resentation(x is an attribute)and in polar representation(x is a function without arguments).In the first case the feature call is a simple field access;in the second it causes a computation to be performed.There is no difference for clients(except possibly in terms of performance).精选53类的使用类的使用有两种形式
24、允引 (class A is a client of class B)继承 (class A is a descendant of class B)Client and supplier a:S in CFeature calla.some_feature or a.some_feature()or infix operatorsSingle target principle精选54Use of the class in a client(1/5)class GRAPHICS featurep,q:POINT-Graphic pointssome_routine is-Use p and q.
25、local u,v:REALdo-Creation instructionscreate pcreate qendend精选0.00.0p(POINT)0.00.0q(POINT)55Use of the class in a client(2/5)class GRAPHICS featurep,q:POINT-Graphic pointssome_routine is-Use p and q.local u,v:REALdo-Creation instructionscreate pcreate qp.move(4.0,-2.0)-Compare with Pascal,C,Ada:-Mov
展开阅读全文