Class and Object课件.ppt

上传人(卖家):三亚风情 文档编号:3228738 上传时间:2022-08-08 格式:PPT 页数:86 大小:907.50KB
下载 相关 举报
Class and Object课件.ppt_第1页
第1页 / 共86页
Class and Object课件.ppt_第2页
第2页 / 共86页
Class and Object课件.ppt_第3页
第3页 / 共86页
Class and Object课件.ppt_第4页
第4页 / 共86页
Class and Object课件.ppt_第5页
第5页 / 共86页
点击查看更多>>
资源描述

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

26、estricted write,through exported procedures.Full write privileges:set_attribute procedure,e.g.set_temperature(u:REAL)is-Set temperature value to u.dotemperature:=uensuretemperature_set:temperature=uend Client will use e.g.x.set_temperature(21.5).Chair of Software Engineering35Other uses of a setter

27、procedureset_temperature(u:REAL)is-Set temperature value to u.requirenot_under_minimum:u=-273not_above_maximum:u=2000dotemperature:=uupdate_databaseensuretemperature_set:temperature=uendChair of Software Engineering36Delphi/C#“properties”Allowx.temperature:=21.5if there is a“setter”:private int temp

28、erature_internal;public int temperatureget return temperature_internal;set temperature_internal=value;/.Other instructions;.Chair of Software Engineering37Information hidingclass A featuref.g.feature NONEh.feature B,Cj.feature A,B,CkendIn clients,with the declaration a1:A,we have:a1.f,a1.g:valid in

29、any client a1.h:invalid anywhere(including in As own text).a1.j:valid only in B,C and their descendants(not valid in A!)a1.k:valid in B,C and their descendants,as well as in A and its descendantsChair of Software Engineering38Information hiding(contd)Information hiding only applies to use by clients

30、,using dot notation or infix notation,as with a1.f(“Qualified calls”).Unqualified calls(within the class itself)are not subject to information hiding:class Afeature NONE h is -Does something.do .endfeature f is -Use h.do .hendend Institute of Computer SoftwareNanjing University39关于子类访问权限关于子类访问权限lC+l

31、JavalEiffel and SmallTalkInstitute of Computer SoftwareNanjing University40模块与类型的统一模块与类型的统一l模块是软件分解的单元,是语法概念l类型是某些动态对象的静态描述,是语义概念l传统语言模块类型分离lC语言的函数指针?l对象语言 统一模块与类型Institute of Computer SoftwareNanjing University41The module-type mergelA class is both:lA module lA type lFrom the module viewpoint:lSet

32、 of available services(“features”).lFrom the type viewpoint:lDescription of set of possible run-time objects(its instances).Institute of Computer SoftwareNanjing University42模块与类型的统一模块与类型的统一l结合继承,威力更增lExtension of the modularlSpecialization of the typelBut How?lEvery object is an instance of some cl

33、ass.lConnection:The services of the class,viewed as a module,are the operations applicable to the instances of the class,viewed as a type.Institute of Computer SoftwareNanjing University43类如何构成系统?类如何构成系统?l允引,允引,再允引l第一驱动问题lRoot class and its creation procedure namel“上帝之手”不应存在于对象软件正文之中。l非集中式的架构l避免直接指定

34、动作顺序Institute of Computer SoftwareNanjing University44其它其它l“纯”面向对象的效率问题l基本类型l对象式程序的风格 小方法l编译优化l效率不那么criticalInstitute of Computer SoftwareNanjing University45OutlinelThree worlds:Object-Oriented ModelinglClass:the static structurelObject:the run-time structurelObject creation:from classes to objects

35、lObject reference:linking up objects Institute of Computer SoftwareNanjing University46例例class A int x;void change1(A old,A cur)old=cur;void change2(A old,A cur)old.x=cur.x;a1,a2 均是均是A的实例,假设他们的的实例,假设他们的x值不同值不同change1(a1,a2);/in Java?in C+?change2(a1,a2);/in Java?in C+?Institute of Computer SoftwareN

36、anjing University47对象系统的运行结构对象系统的运行结构l对象:A run-time instance of some class.l某对象O是某类C的(直接)实例lO包含为C中定义的属性(数据成员)l当前状态(O的fields)l运行规律类定义的行为l面向对象的软件系统运行时由一组对象构成。l对象是对问题域对象,并进而对现实对象的实现,l三种对象概念上的一致性与差异性 Institute of Computer SoftwareNanjing University48对象结构对象结构l回头看Point类 和 point 对象l简单类型的域与引用类型的域Institute o

37、f Computer SoftwareNanjing University49Institute of Computer SoftwareNanjing University50Institute of Computer SoftwareNanjing University51Institute of Computer SoftwareNanjing University52Institute of Computer SoftwareNanjing University53对象对象l对象引用:lA reference is a run-time value which is either vo

38、id or attached.lIf attached,a reference identifies a single object(be attached to this object)l对象的identityl不同对象可有完全一样的域l一个对象的域可随系统执行而变化Institute of Computer SoftwareNanjing University54对象对象l引用声明lclass C feature.Endlx:Cl看前面点的例子lSelf reference is possibleInstitute of Computer SoftwareNanjing Universit

39、y55运行时刻结构运行时刻结构Institute of Computer SoftwareNanjing University56OutlinelThree worlds:Object-Oriented ModelinglClass:the static structurelObject:the run-time structurelObject creation:from classes to objectslObject reference:linking up objects Institute of Computer SoftwareNanjing University57Object

40、 creationl对象按需创建,显式创建。l传统技术往往基于栈分配实体l运行时刻对象动态结构多变,而难以根据程序文本预测lEiffel 的对象创建l基本创建l基本创建 初始化featurelCreation proceduresChair of Software Engineering58Creating an object With the class POINT as given:my_point:POINT.create my_point Effect of such a creation instruction:Allocate new object of the type decl

41、ared for my_point.Initialize its fields to default values(0 for numbers,false for booleans,null for characters,void for references).Attach it to the instructions target,here my_point.Chair of Software Engineering59Specific creation proceduresclass POINT createmake_cartesian,make_polarfeature-Initial

42、izationmake_cartesian(a,b:REAL)is-Initialize to abscissa a,ordinate b.dox:=ay:=bendmake_polar.feature.The rest as before.Chair of Software Engineering60If there is a creation clause Creation instructions must be“creation calls”,such ascreate my_point.make_polar(1,Pi/2)Chair of Software Engineering61

43、If there is no creation clauseAn absent creation clause,as inclass POINT-No creation clausefeature The rest as before endis understood as one that would only list default_create,as if it had been writtenclass POINT createdefault_createfeature The rest as before endProcedure default_create is defined

44、 in ANY as doing nothing;any class can redefine it to provide proper default initializations.Chair of Software Engineering62Associated convention The notationcreate xis understood(if permitted)as an abbreviation forcreate x.default_createChair of Software Engineering63To allow both formsTo make both

45、 forms valid:create my_pointas well ascreate my_point.make_polar(1,Pi/2)it suffices to make default_create(redefined or not)one of the creation procedures:class POINT createmake_cartesian,make_polar,default_createfeature.The rest as before.Chair of Software Engineering64To prohibit instantiating a c

46、lassclass NOT_CREATABLE create-Nothing here!feature.The rest as before.endInstitute of Computer SoftwareNanjing University65OutlinelThree worlds:Object-Oriented ModelinglClass:the static structurelObject:the run-time structurelObject creation:from classes to objectslObject reference:linking up objec

47、ts Chair of Software Engineering66The dynamic model States of a reference:Operations on references:create pp:=qp:=Voidif p=Void then.VOIDATTACHEDcreate pp:=q(where q is attached)p:=Voidp:=q(where q is void)pATTACHEDpVOIDInstitute of Computer SoftwareNanjing University67Object referencel回顾C+和Java的相应机

48、制lVoid Reference,clone,copy and comparel对象引用的问题l引用类型与扩展类型 Chair of Software Engineering68Forms of assignment and copy Reference assignment(a and b of reference types):b:=a Object duplication(shallow):c:=clone(a)Object duplication(deep):d:=deep_clone(a)Also:shallow field-by-field copy(no new object i

49、s created):e.copy(a)Chair of Software Engineering69Shallow and deep cloning Initial situation:Result of:b:=ac:=clone(a)d:=deep_clone(a)“Almaviva”namelandlordloved_oneaO1“Figaro”O2“Susanna”O3b“Almaviva”O4c“Almaviva”namelandlordloved_oneO5“Figaro”O6“Susanna”O7dChair of Software Engineering70A related

50、mechanism:Persistencea.store(file).b?=retrieved(file)Storage is automatic.Persistent objects identified individually by keys.These features come from the library class STORABLE.The above is only an approximate form(see typing discussion).Institute of Computer SoftwareNanjing University71Object refer

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 办公、行业 > 各类PPT课件(模板)
版权提示 | 免责声明

1,本文(Class and Object课件.ppt)为本站会员(三亚风情)主动上传,163文库仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。
2,用户下载本文档,所消耗的文币(积分)将全额增加到上传者的账号。
3, 若此文所含内容侵犯了您的版权或隐私,请立即通知163文库(发送邮件至3464097650@qq.com或直接QQ联系客服),我们立即给予删除!


侵权处理QQ:3464097650--上传资料QQ:3464097650

【声明】本站为“文档C2C交易模式”,即用户上传的文档直接卖给(下载)用户,本站只是网络空间服务平台,本站所有原创文档下载所得归上传人所有,如您发现上传作品侵犯了您的版权,请立刻联系我们并提供证据,我们将在3个工作日内予以改正。


163文库-Www.163Wenku.Com |网站地图|