类和对象范本学习培训模板课件.ppt

上传人(卖家):林田 文档编号:3274229 上传时间:2022-08-15 格式:PPT 页数:94 大小:2.26MB
下载 相关 举报
类和对象范本学习培训模板课件.ppt_第1页
第1页 / 共94页
类和对象范本学习培训模板课件.ppt_第2页
第2页 / 共94页
类和对象范本学习培训模板课件.ppt_第3页
第3页 / 共94页
类和对象范本学习培训模板课件.ppt_第4页
第4页 / 共94页
类和对象范本学习培训模板课件.ppt_第5页
第5页 / 共94页
点击查看更多>>
资源描述

1、类和对象Class and Object关于对象式语言2022-8-15Institute of Computer Software,Nanjing University2本课程并不系统讲授Eiffel语言但应学会“欣赏”Eiffel 语言(比较“纯”)它首先是一个对象式程序设计语言但强调从分析到设计到实现的平滑过渡摘要2022-8-15Institute of Computer SoftwareNanjing University3三个世界:面向对象的建模面向对象原则类:对象程序静态的结构对象:运行时刻的结构对象创建:从类到对象对象引用:对象串起来摘要三个世界三个世界:面向对象的建模面向对象

2、的建模面向对象原则类:对象程序静态的结构对象:运行时刻的结构对象创建:从类到对象对象引用:对象串起来2022-8-15Institute of Computer Software,Nanjing University4三个世界2022-8-15Institute of Computer Software,Nanjing University5客观世界 DVD播放机What is reality?复杂性 认识的主观性 问题世界抽象!A model of a subset of the reality DVD播放机计算机(软件)世界A model of a model of a subset of

3、 the reality2022-8-1562022-8-15Institute of Computer SoftwareNanjing University现实世界现实世界问题世界问题世界软件世界软件世界Reality抽象抽象摘要2022-8-15Institute of Computer SoftwareNanjing University7三个世界:面向对象的建模面向对象原则面向对象原则类:对象程序静态的结构对象:运行时刻的结构对象创建:从类到对象对象引用:对象串起来面向对象原则2022-8-15Institute of Computer Software,Nanjing Univers

4、ity8Object OrientationEncapsulationAbstractionPolymorphismInheritance面向对象原则2022-8-15Institute of Computer Software,Nanjing University9Object OrientationEncapsulationAbstractionPolymorphismInheritance回顾:抽象数据类型2022-8-15Institute of Computer Software,Nanjing University10抽象数据类型(ADT):用数学方法定义对象集合和运算集合,仅通过

5、运算的性质刻画数据对象,而独立于计算机中可能的表示方法ADT规约方法代数规范语法部分,公理部分ADT实现E1,E2,E3面向对象原则2022-8-15Institute of Computer Software,Nanjing University11Object OrientationEncapsulationAbstractionPolymorphismInheritance封装2022-8-15Institute of Computer Software,Nanjing University12封装是把过程和数据包围起来,对数据的访问只能通过已定义的界面。DVD播放机2022-8-15I

6、nstitute of Computer SoftwareNanjing University13封装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

7、:4SetMaxLoad(4)2022-8-15Institute of Computer SoftwareNanjing University14封装信息/实现隐藏Deposit()Withdraw()Transfer()BalanceinsterestYTDOwnerAccount_number Deposit()Withdraw()Transfer()2022-8-15Institute of Computer SoftwareNanjing University15封装封装使得对象在抽象所界定的范围内保持相对的独立,从而保证对象设计和对象实施方法的可靠分离。设计者和使用者分离!封装可以

8、防止由于程序模块之间的互相依赖性而导致程序“不稳定”不会因某个部分的修改而导致整个系统许多部分都发生改动。封装是相对的!不能也没有必要阻止编程人员去查看实施细节。2022-8-15Institute of Computer SoftwareNanjing University16封装 vs.继承矛盾吗?No!封装性主要指的是对象的封装性,引入继承机制后,对象仍然是封装地很好的实体,其它对象与它通信只能发送消息。相似性:共享代码!继承:静态共享代码封装:动态共享代码2022-8-15Institute of Computer SoftwareNanjing University17模块化传统的模

9、块划分方法:要求组成的子程序(模块)有实际意义和明确的某个功能功能分解和结构分解OO的模块划分目标:生成一组有助于降低系统开发费用的模块,通过模块划分所得到的模块应该能够保证可以独立的生成和修改,每个模块的结构应该足够简单2022-8-15Institute of Computer SoftwareNanjing University18模块化与抽象和封装密切相关模块的划分是以抽象出来的对象的性质和功能为依据的模块是封装得以实现的技术保障Booch:“模块化是反映一个被分解成一组相关联的松散结合模块组成系统的一个特性。”模块化以模块为基础2022-8-15Institute of Comput

10、er SoftwareNanjing University19模块与类型的统一模块是软件分解的单元,是语法概念类型是某些动态对象的静态描述,是语义概念传统语言 模块与类型分离对象语言 模块与类型统一类型:类是抽象数据类型的实现模块:类是对象式程序的基本组成单元基于类的面向对象的语言机制的强有力之处在于“类”统一了类型和模块面向对象原则2022-8-15Institute of Computer Software,Nanjing University20Object OrientationEncapsulationAbstractionPolymorphismInheritance2022-8-

11、15Institute of Computer SoftwareNanjing University21层次性封装性帮助隐藏细节;模块化使结构更加有序,但仍然不够!层次性是对抽象的排序和定位类结构关系(“is a”)对象结构关系(“part of”)实现方式继承:子类,父类单继承,多继承聚合:拥有关系/组合关系2022-8-15Institute of Computer SoftwareNanjing University22继承单继承CheckingSavingsSuperclass(parent)SubclassesInheritance RelationshipAncestorDesce

12、ndentsAccount-balance-name-number+withdraw()+createStatement()2022-8-15Institute of Computer SoftwareNanjing University23继承多继承Use multiple inheritance only when needed and always with caution!FlyingThingAnimalHorseWolfBirdHelicopterAirplaneMultiple Inheritance2022-8-15Institute of Computer SoftwareN

13、anjing University24继承继承 作为模块扩展机制,体现开闭原则开闭原则,较好地支持软件的复用。继承 作为类型特化机制,体现分类方法,更合理地支持子类型实现。继承也是多态和动态绑定机制的基础。2022-8-15Institute of Computer SoftwareNanjing University25子类对父类的扩展与特化继承表达的是一种分类演绎的认识方法“To program is to understand.”特化:包含范围变小,行为特殊化扩展:增加新的操作重定义:改变行为2022-8-15Institute of Computer SoftwareNanjing U

14、niversity26The Open-Closed Principle(OCP)动机:软件是变化的!开闭原则:“Closed for Modification;Open for Extension”对修改关闭,对扩展开放!Why OCP?-重用How OCP?-继承The 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 pr

15、ogram unit)compiled.The rationales are complementary:For closing a module(managers perspective):Clients need it now.For keeping modules open(developers perspective):One frequently overlooks aspects of the problem.2022-8-15Institute of Computer Software,Nanjing University27The Open-Closed Principle(1

16、)Institute of Computer SoftwareNanjing UniversityACEDB2022-8-15The Open-Closed Principle(2)Institute of Computer SoftwareNanjing UniversityACEDBFAGH I2022-8-15The Open-Closed Principle(3)Institute of Computer SoftwareNanjing UniversityACEDBFAGH I2022-8-152022-8-15Institute of Computer SoftwareNanjin

17、g University31Example面向对象原则2022-8-15Institute of Computer Software,Nanjing University32Object OrientationEncapsulationAbstractionPolymorphismInheritance2022-8-15Institute of Computer SoftwareNanjing University33多态多态性使指相同的操作或函数、过程可作用于多种类型的对象上并获得不同的结果。不同的对象,收到同一消息可以产生不同的结果,这种现象称为多态性。多态性允许每个对象以适合自身的方式去

18、响应共同的消息多态性增强了软件的灵活性和重用性。2022-8-15Institute of Computer SoftwareNanjing University34多态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 Square();case 2:return new Triangl

19、e();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=0end2022-8-15Institute of Computer Software,Nanjing University40类的构成类名继承关系成员术语分类2022-8-15Institute of Computer Software,Nanjing University41Abstract data type POINTx:

20、POINT REALy:POINT REAL:POINT REAL:POINT REALClass POINT:Choose a representation(polar,cartesian)In polar representation,and are attributes,x and y are routines.2022-8-15Institute of Computer Software,Nanjing University42yxA simple classclass POINT featurex,y:REAL-Point cartesian coordinatesmove(a,b:

21、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*yend2022-8-15Institute of Computer Software,Nanjing University43Class POINT(contd)distance(p:POINT):REAL is-Distance to pdoResult:=sqrt(x p.x)2+(y p.y)2)endro:REAL is-Dista

22、nce to origin(0,0)doResult:=sqrt(x2+y2)endtheta:REAL is-Angle to horizontal axisdoendend2022-8-15Institute of Computer Software,Nanjing University44术语A class is an implementation of an abstract data type.Instances of the class may be created at run-time;they are objects.类的实例在运行时刻产生,他们是对象。Every objec

23、t 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.2022-8-15Institute of Computer Software,Nanjing University45术语A class is characterized

24、 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 attribute call)is relative t

25、o a distinguished object,the current instance of the class.2022-8-15Institute of Computer Software,Nanjing University46Feature categories by role2022-8-15Institute of Computer Software,Nanjing University47CommandQueryFeatureProcedureAttributeFunctionNo resultReturns resultComputationMemoryFeature ca

26、tegories by implementation2022-8-15Institute of Computer Software,Nanjing University48ProcedureAttributeFunctionRoutineReturns resultNo resultMemoryComputationFeatureFeature categories2022-8-15Institute of Computer Software,Nanjing University49CommandQueryFeatureProcedureAttributeFunctionNo resultRe

27、turns resultComputationMemoryRoutineReturns resultNo resultMemoryComputationFeatureAlternative terminologyAttributes are also called instance variables or data member.Routines are also called methods,subprograms,or subroutines.Feature call applying a certain feature of a class to an instance of that

28、 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 the Uniform Access principle.2

29、022-8-15Institute of Computer Software,Nanjing University50回顾:Uniform Access 访问一致性原则客户应能以实现无关的方式访问模块的功能,不管这个功能是通过存储还是计算来实现的。Facilities managed by a module must be accessible to clients in the same way whether implemented by computation or storage.2022-8-15Institute of Computer Software,Nanjing Unive

30、rsity51Uniform Accessbalance=list_of_deposits.total list_of_withdrawals.total2022-8-15Institute of Computer SoftwareNanjing University52list_of_depositslist_of_withdrawalsbalancelist_of_depositslist_of_withdrawals(A2)(A1)Uniform access through feature callTo access a property of a point p1,the notat

31、ion 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 feature call is a simple field access;in the second it causes a computation to be perfo

32、rmed.There is no difference for clients(except possibly in terms of performance).2022-8-15Institute of Computer Software,Nanjing University53类的使用类的使用有两种形式允引 (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

33、()or infix operatorsSingle target principle2022-8-15Institute of Computer Software,Nanjing University54Use 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 qendend2022-8-15Institute of Computer

34、 Software,Nanjing University550.00.0p(POINT)0.00.0q(POINT)Use 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:-Move(p,4.0,-2.0)endend2022-8-15Instit

35、ute of Computer Software,Nanjing University564.0-2.0p(POINT)0.00.0q(POINT)Use of the class in a client(3/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.sca

36、le(0.5)endend2022-8-15Institute of Computer Software,Nanjing University572.0-1.0p(POINT)0.00.0q(POINT)Use of the class in a client(4/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,

37、C,Ada:-Move(p,4.0,-2.0)p.scale(0.5)u:=p.distance(q)v:=p.xp:=qendend2022-8-15Institute of Computer Software,Nanjing University582.0-1.0p(POINT)0.00.0q(POINT)Use of the class in a client(5/5)class GRAPHICS featurep,q:POINT-Graphic pointssome_routine is-Use p and q.local u,v:REALdo-Creation instruction

38、screate 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)endend2022-8-15Institute of Computer Software,Nanjing University592.0-1.0p(POINT)0.00.0q(POINT)2022-8-15Institute of Computer SoftwareNanjing University60模块与类型的统一从模块的观点:Set

39、 of available services(features or methods).从类型的观点:Description of set of possible run-time objects(its instances).如何做到?Every object is an instance of some class.Connection:The services of the class,viewed as a module,are the operations applicable to the instances of the class,viewed as a type.Applyi

40、ng abstraction principlesPrivileges 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.2022-8-15Institute of Computer Software,Nanjing University61CAa1:AThe privileges of a client2022-8-15Institute of Computer Software,

41、Nanjing University62SecretRead-onlyRead,restricted writeFull writeApplying abstraction principlesBeyond read access:full or restricted write,through exported procedures.Full write privileges:set_attribute procedure,e.g.set_temperature(u:REAL)is-Set temperature value to u.dotemperature:=uensuretemper

42、ature_set:temperature=uendClient will use e.g.x.set_temperature(21.5).2022-8-15Institute of Computer Software,Nanjing University63Other uses of a setter procedureset_temperature(u:REAL)is-Set temperature value to u.requirenot_under_minimum:u=-273not_above_maximum:u=2000dotemperature:=uupdate_databas

43、eensuretemperature_set:temperature=uend2022-8-15Institute of Computer Software,Nanjing University64Delphi/C#“properties”Allowx.temperature:=21.5if there is a“setter”:private int temperature_internal;public int temperatureget return temperature_internal;set temperature_internal=value;/.Other instruct

44、ions;.2022-8-15Institute of Computer Software,Nanjing University65Information hiding2022-8-15Institute of Computer Software,Nanjing University66class 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 any client a1.h:invalid anywh

45、ere(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 descendantsInformation hiding(contd)Information hiding only applies to use by clients,using dot notation or infix notation,as with a1.f(“Qualifie

46、d 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 2022-8-15Institute of Computer Software,Nanjing University67DVD播放机摘要2022-8-15Institute of Computer SoftwareNanjing University68三个世

47、界:面向对象的建模面向对象原则类:对象程序静态的结构对象对象:运行时刻的结构:运行时刻的结构对象创建:从类到对象对象引用:对象串起来对象:系统的运行结构对象:A run-time instance of some class.某对象O是某类C的(直接)实例O包含为C中定义的属性(数据成员)当前状态(O的fields)运行规律类定义的行为面向对象的软件系统运行时由一组对象构成。对象是对问题域对象,并进而对现实对象的实现,三种对象概念上的一致性与差异性 2022-8-15Institute of Computer Software,Nanjing University69DVD播放机对象有状态Th

48、e state of an object is one of the possible conditions in which an object may exist.The state of an object normally changes over time.2022-8-15Institute of Computer Software,Nanjing University70Name:J ClarkEmployee ID:567138Date Hired:July 25,1991Status:TenuredDiscipline:FinanceMaximum Course Load:3

49、 classesProfessor Clark对象有行为Behavior determines how an object acts and reacts.The visible behavior of an object is modeled by the set of messages it can respond to(operations the object can perform).2022-8-15Institute of Computer Software,Nanjing University71SubmitFinalGrades()AcceptCourseOffering()

50、TakeSabbatical()SetMaxLoad()Name:J ClarkEmployee ID:567138HireDate:07/25/1991Status:TenuredDiscipline:FinanceMaxLoad:3Professor Clark对象有标识Each object has a unique identity,even if the state is identical to that of another object.2022-8-15Institute of Computer Software,Nanjing University72Professor“J

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

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

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


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

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


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