Class and Object课件.ppt
- 【下载声明】
1. 本站全部试题类文档,若标题没写含答案,则无答案;标题注明含答案的文档,主观题也可能无答案。请谨慎下单,一旦售出,不予退换。
2. 本站全部PPT文档均不含视频和音频,PPT中出现的音频或视频标识(或文字)仅表示流程,实际无音频或视频文件。请谨慎下单,一旦售出,不予退换。
3. 本页资料《Class and Object课件.ppt》由用户(三亚风情)主动上传,其收益全归该用户。163文库仅提供信息存储空间,仅对该用户上传内容的表现方式做保护处理,对上传内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知163文库(点击联系客服),我们立即给予删除!
4. 请根据预览情况,自愿下载本文。本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
5. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007及以上版本和PDF阅读器,压缩文件请下载最新的WinRAR软件解压。
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Class and Object课件 Object 课件
- 资源描述:
-
1、Class and Object马晓星南京大学计算机软件研究所南京大学计算机科学与技术系Institute of Computer SoftwareNanjing University2关于对象式语言关于对象式语言l本课程并不系统讲授Eiffel语言l但应学会“欣赏”Eiffel 语言(比较“纯”)l它首先是一个对象式程序设计语言l但强调从分析到设计到实现的平滑过渡l本课程主要讨论基于“类”的面向对象语言,但另有一些语言并不基于“类”概念。Institute of Computer SoftwareNanjing University3OutlinelThree worlds:Object-O
2、riented ModelinglClass:the static structurelObject:the run-time structurelObject creation:from classes to objectslObject reference:linking up objects Institute of Computer SoftwareNanjing University4OutlinelThree worlds:Object-Oriented ModelinglClass:the static structurelObject:the run-time structur
3、elObject creation:from classes to objectslObject reference:linking up objects Institute of Computer SoftwareNanjing University5Three worldsl客观世界 DVD播放机lWhat is reality?l复杂性 认识的主观性 l问题世界l抽象!Tell me not what you are but what you havelA model of a subset of the reality DVD播放机l计算机(软件)世界lA model of a mod
4、el of a subset of the realityInstitute of Computer SoftwareNanjing University6现实世界现实世界问题世界问题世界软件世界软件世界RealityAbstractInstitute of Computer SoftwareNanjing University7OutlinelThree worlds:Object-Oriented ModelinglClass:the static structurelObject:the run-time structurelObject creation:from classes to
5、 objectslObject reference:linking up objects Institute of Computer SoftwareNanjing University8Class:the static structurel类是对象式软件系统的基本组成单元l类的构成l类的使用l信息隐蔽设施l模块与类型l类如何构成系统?l其它Institute of Computer SoftwareNanjing University9Classl“A class is an abstract data type equipped with a possibly partial implem
6、entation.”lDeferred and effective class lBut why?Chair of Software Engineering10A very deferred classdeferred class COUNTER featureitem:INTEGER is deferred end-Counter valueup is-Increase item by 1.deferredensureitem=old item+1enddown is-Decrease item by 1.deferredensureitem=old item 1endinvariantit
7、em=0endInstitute of Computer SoftwareNanjing University11ClasslMold and instancel类自身也能被当作对象么Institute of Computer SoftwareNanjing University12类的构成类的构成l类名l继承关系l成员l术语l分类Chair of Software Engineering13Abstract data type POINTx:POINT REALy:POINT REAL:POINT REAL:POINT REAL Class POINT:Choose a representa
8、tion(polar,cartesian)In polar representation,and are attributes,x and y are routines.yxChair of Software Engineering14A 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
9、.dox:=factor*xy:=factor*yendChair of Software Engineering15Class 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 axisdoendendChair of Software Engineering16Terminology A
10、class is characterized by features.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 attrib
11、ute call)is relative to a distinguished object,the current instance of the class.Chair of Software Engineering17Alternative terminology Attributes are also called instance variables or data member.Routines are also called methods,subprograms,or subroutines.Feature call applying a certain feature of
12、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 attribute or a routine as expressed by
13、the Uniform Access principleChair of Software Engineering18Feature categories by roleCommandQueryFeatureProcedureAttributeFunctionNo resultReturns resultComputationMemoryChair of Software Engineering19Feature categories by implementationProcedureAttributeFunctionRoutineReturns resultNo resultFeature
14、MemoryComputationChair of Software Engineering20Feature categoriesCommandQueryFeatureProcedureAttributeFunctionNo resultReturns resultComputationMemoryRoutineReturns resultNo resultFeatureMemoryComputationChair of Software Engineering21Uniform Accessbalance=list_of_deposits.total list_of_withdrawals
15、.totallist_of_depositslist_of_withdrawalsbalancelist_of_depositslist_of_withdrawals(A2)(A1)Chair of Software Engineering22The Principle of Uniform Access Facilities managed by a module must be accessible to clients in the same way whether implemented by computation or storage.Chair of Software Engin
16、eering23Uniform access through feature call To 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 representation(x is an attribute)and in polar representation(x is a function without arguments).In the first case the
17、 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).Institute of Computer SoftwareNanjing University24关于关于C+/Java的静态成员的静态成员l有力的设施l但并非面向对象计算所必须?l不修改静态数据成员的l修改静态数据成员的l破坏面向对象的“纯粹性”lsingl
18、e target principle(see below)l考虑类作为对象?Institute of Computer SoftwareNanjing University25类的使用类的使用l类的使用有两种形式l允引 (class A is a client of class B)l继承 (class A is a descendant of class B)l Client and supplier la:S in ClFeature callla.some_feature or a.some_feature()or infix operatorslSingle target princi
19、pleChair of Software Engineering26Use of the class in a client(1/5)class GRAPHICS featurep,q:POINT-Graphic pointssome_routine is-Use p and q.local u,v:REALdo-Creation instructionscreate pcreate qendend0.00.0p(POINT)0.00.0q(POINT)Chair of Software Engineering27Use of the class in a client(2/5)class G
20、RAPHICS 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:-Move(p,4.0,-2.0)endend4.0-2.0p(POINT)0.00.0q(POINT)Chair of Software Engineering28Use of the class in a client(3/5)class GRAPHICS featu
21、rep,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:-Move(p,4.0,-2.0)p.scale(0.5)endend2.0-1.0p(POINT)0.00.0q(POINT)Chair of Software Engineering29Use of the class in a client(4/5)class GRAPHICS featur
22、ep,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:-Move(p,4.0,-2.0)p.scale(0.5)u:=p.distance(q)v:=p.xp:=qendend2.0-1.0p(POINT)0.00.0q(POINT)Chair of Software Engineering30Use of the class in a client(
23、5/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:-Move(p,4.0,-2.0)p.scale(0.5)u:=p.distance(q)v:=p.xp:=qp.scale(-3.0)endend2.0-1.0p(POINT)0.00.0q(POINT)Institute of Computer
24、SoftwareNanjing University31类的信息隐蔽设施类的信息隐蔽设施l回忆 C+和Java的信息隐蔽设施l看Eiffel的设计l细粒度的设计Chair of Software Engineering32Applying abstraction principles Privileges of a client C of a class A on an attribute attrib:Read access if attribute is exported.Assuming a1:AThen a1.attrib is an expression.An assignment
25、such as a1.attrib:=a2 is syntactically illegal!(You cannot assign a value to an expression,e.g.x+y.)CAa1:AChair of Software Engineering33The privileges of a clientSecretRead-onlyRead,restricted writeFull writeChair of Software Engineering34Applying abstraction principles Beyond read access:full or r
展开阅读全文