类和对象.ppt

上传人:文库蛋蛋多 文档编号:4525842 上传时间:2023-04-26 格式:PPT 页数:94 大小:2.26MB
返回 下载 相关 举报
类和对象.ppt_第1页
第1页 / 共94页
类和对象.ppt_第2页
第2页 / 共94页
类和对象.ppt_第3页
第3页 / 共94页
类和对象.ppt_第4页
第4页 / 共94页
类和对象.ppt_第5页
第5页 / 共94页
点击查看更多>>
资源描述

《类和对象.ppt》由会员分享,可在线阅读,更多相关《类和对象.ppt(94页珍藏版)》请在三一办公上搜索。

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

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

3、 subset of the reality,2023/4/26,6,2023/4/26,Institute of Computer SoftwareNanjing University,现实世界,问题世界,软件世界,Reality,抽象,摘要,2023/4/26,Institute of Computer SoftwareNanjing University,7,三个世界:面向对象的建模面向对象原则类:对象程序静态的结构对象:运行时刻的结构对象创建:从类到对象对象引用:对象串起来,面向对象原则,2023/4/26,Institute of Computer Software,Nanjing

4、University,8,面向对象原则,2023/4/26,Institute of Computer Software,Nanjing University,9,回顾:抽象数据类型,2023/4/26,Institute of Computer Software,Nanjing University,10,抽象数据类型(ADT):用数学方法定义对象集合和运算集合,仅通过运算的性质刻画数据对象,而独立于计算机中可能的表示方法ADT规约方法代数规范语法部分,公理部分ADT实现E1,E2,E3,面向对象原则,2023/4/26,Institute of Computer Software,Nanj

5、ing University,11,封装,2023/4/26,Institute of Computer Software,Nanjing University,12,封装是把过程和数据包围起来,对数据的访问只能通过已定义的界面。,DVD播放机,2023/4/26,Institute of Computer SoftwareNanjing University,13,封装,Professor Clark needs to be able to teach four classes in the next semester.,SubmitFinalGrades(),AcceptCourseOff

6、ering(),TakeSabbatical(),Professor Clark,SetMaxLoad(),Name:J ClarkEmployee ID:567138HireDate:07/25/1991Status:TenuredDiscipline:FinanceMaxLoad:4,SetMaxLoad(4),2023/4/26,Institute of Computer SoftwareNanjing University,14,封装信息/实现隐藏,Interface,Client,Deposit()Withdraw()Transfer(),BalanceinsterestYTDOwn

7、erAccount_number Deposit()Withdraw()Transfer(),2023/4/26,Institute of Computer SoftwareNanjing University,15,封装,封装使得对象在抽象所界定的范围内保持相对的独立,从而保证对象设计和对象实施方法的可靠分离。设计者和使用者分离!封装可以防止由于程序模块之间的互相依赖性而导致程序“不稳定”不会因某个部分的修改而导致整个系统许多部分都发生改动。封装是相对的!不能也没有必要阻止编程人员去查看实施细节。,2023/4/26,Institute of Computer SoftwareNanjing

8、 University,16,封装 vs.继承,矛盾吗?No!封装性主要指的是对象的封装性,引入继承机制后,对象仍然是封装地很好的实体,其它对象与它通信只能发送消息。相似性:共享代码!继承:静态共享代码封装:动态共享代码,2023/4/26,Institute of Computer SoftwareNanjing University,17,模块化,传统的模块划分方法:要求组成的子程序(模块)有实际意义和明确的某个功能功能分解和结构分解OO的模块划分目标:生成一组有助于降低系统开发费用的模块,通过模块划分所得到的模块应该能够保证可以独立的生成和修改,每个模块的结构应该足够简单,2023/4/

9、26,Institute of Computer SoftwareNanjing University,18,模块化,与抽象和封装密切相关模块的划分是以抽象出来的对象的性质和功能为依据的模块是封装得以实现的技术保障Booch:“模块化是反映一个被分解成一组相关联的松散结合模块组成系统的一个特性。”模块化以模块为基础,2023/4/26,Institute of Computer SoftwareNanjing University,19,模块与类型的统一,模块是软件分解的单元,是语法概念类型是某些动态对象的静态描述,是语义概念传统语言 模块与类型分离对象语言 模块与类型统一类型:类是抽象数据类

10、型的实现模块:类是对象式程序的基本组成单元,基于类的面向对象的语言机制的强有力之处在于“类”统一了类型和模块,面向对象原则,2023/4/26,Institute of Computer Software,Nanjing University,20,2023/4/26,Institute of Computer SoftwareNanjing University,21,层次性,封装性帮助隐藏细节;模块化使结构更加有序,但仍然不够!层次性是对抽象的排序和定位类结构关系(“is a”)对象结构关系(“part of”)实现方式继承:子类,父类单继承,多继承聚合:拥有关系/组合关系,2023/4/

11、26,Institute of Computer SoftwareNanjing University,22,继承,单继承,Checking,Savings,Superclass(parent),Subclasses,Inheritance Relationship,Ancestor,Descendents,2023/4/26,Institute of Computer SoftwareNanjing University,23,继承,多继承,Use multiple inheritance only when needed and always with caution!,Multiple

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

13、作重定义:改变行为,2023/4/26,Institute of Computer SoftwareNanjing University,26,The Open-Closed Principle(OCP),动机:软件是变化的!开闭原则:“Closed for Modification;Open for Extension”对修改关闭,对扩展开放!Why OCP?-重用How OCP?-继承,The Open-Closed Principle,Modules should be open and closed.Definitions:Open module:May be extended.Clo

14、sed 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 modules open(developers perspective):One frequently overlooks aspects of the problem.,2023/4/26,Institute of

15、 Computer Software,Nanjing University,27,The Open-Closed Principle(1),Institute of Computer SoftwareNanjing University,A,C,E,D,B,2023/4/26,The Open-Closed Principle(2),Institute of Computer SoftwareNanjing University,A,C,E,D,B,F,A,G,H,I,2023/4/26,The Open-Closed Principle(3),Institute of Computer So

16、ftwareNanjing University,A,C,E,D,B,F,A,G,H,I,2023/4/26,2023/4/26,Institute of Computer SoftwareNanjing University,31,Example,面向对象原则,2023/4/26,Institute of Computer Software,Nanjing University,32,2023/4/26,Institute of Computer SoftwareNanjing University,33,多态,多态性使指相同的操作或函数、过程可作用于多种类型的对象上并获得不同的结果。不同的

17、对象,收到同一消息可以产生不同的结果,这种现象称为多态性。多态性允许每个对象以适合自身的方式去响应共同的消息多态性增强了软件的灵活性和重用性。,2023/4/26,Institute of Computer SoftwareNanjing University,34,多态,Shape,Circle,Triangle,Square,draw(),draw(),draw(),draw(),public class Shapes public static Shape randShape()switch(int)(Math.random()*3)default:case 0:return new C

18、ircle();case 1:return new Square();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 s.length;i+)si.draw();,2023/4/26,Institute of Computer SoftwareNanjing University,35,多态和动态绑定,通过类继承机制实现子类型,

19、允许子类重定义父类的操作。这样父类型变量(对象引用)及其上的的操作必然是多态的。这就在实现上要求动态绑定机制,2023/4/26,Institute of Computer SoftwareNanjing University,36,动态绑定,绑定:就是建立method call和method body(方法本体)的关联。静态绑定:指绑定动作发生于程序执行前(由编译器和连接器完成)。动态绑定:指绑定动作将在执行期间才根据对象类型而进行,不同的对象类型将作出不同的编译结果。具有灵活性,但效率有一点损失。找到最合适的method body,摘要,2023/4/26,Institute of Com

20、puter SoftwareNanjing University,37,三个世界:面向对象的建模面向对象原则类:对象程序静态的结构对象:运行时刻的结构对象创建:从类到对象对象引用:对象串起来,类:对象程序静态的结构,类是对象式软件系统的基本组成单元类的构成类的使用信息隐蔽设施模块与类型,2023/4/26,Institute of Computer Software,Nanjing University,38,Class,一般说类是抽象数据类型的实现抽象数据类型乃是一种规约;类是OOPL实现这种类型的设施;但是Meyer说:“A class is an abstract data type e

21、quipped with a possibly partial implementation.”Meyer在Eiffel中强调将ADT规约作为类的一部分强调从规约到实现的一致表达和平滑过度类可能只是部分实现Deferred and effective class,2023/4/26,Institute of Computer Software,Nanjing University,39,A very deferred class,deferred class COUNTER Featureitem:INTEGER is deferred end-Counter valueup is-Incre

22、ase item by 1.deferredensureitem=old item+1enddown is-Decrease item by 1.deferredensureitem=old item 1endinvariantitem=0end,2023/4/26,Institute of Computer Software,Nanjing University,40,类的构成,类名继承关系成员术语分类,2023/4/26,Institute of Computer Software,Nanjing University,41,Abstract data type POINT,x:POINT

23、 REALy:POINT REAL:POINT REAL:POINT REALClass POINT:Choose a representation(polar,cartesian)In polar representation,and are attributes,x and y are routines.,2023/4/26,Institute of Computer Software,Nanjing University,42,y,x,A simple class,class POINT featurex,y:REAL-Point cartesian coordinatesmove(a,

24、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,2023/4/26,Institute of Computer Software,Nanjing University,43,Class POINT(contd),distance(p:POINT):REAL is-Distance to pdoResult:=sqrt(x p.x)2+(y p.y)2)endro:REAL i

25、s-Distance to origin(0,0)doResult:=sqrt(x2+y2)endtheta:REAL is-Angle to horizontal axisdoendend,2023/4/26,Institute of Computer Software,Nanjing University,44,术语,A class is an implementation of an abstract data type.Instances of the class may be created at run-time;they are objects.类的实例在运行时刻产生,他们是对象

26、。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.,2023/4/26,Institute of Computer Software,Nanjing University,45,术语,A class

27、 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 attribute c

28、all)is relative to a distinguished object,the current instance of the class.,2023/4/26,Institute of Computer Software,Nanjing University,46,Feature categories by role,2023/4/26,Institute of Computer Software,Nanjing University,47,Command,Query,Feature,Procedure,Attribute,Function,No result,Returns r

29、esult,Computation,Memory,Feature categories by implementation,2023/4/26,Institute of Computer Software,Nanjing University,48,Procedure,Attribute,Function,Routine,Returns result,No result,Memory,Computation,Feature,Feature categories,2023/4/26,Institute of Computer Software,Nanjing University,49,Comm

30、and,Query,Feature,Procedure,Attribute,Function,No result,Returns result,Computation,Memory,Routine,Returns result,No result,Memory,Computation,Feature,Alternative terminology,Attributes are also called instance variables or data member.Routines are also called methods,subprograms,or subroutines.Feat

31、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

32、attribute or a routine as expressed by the Uniform Access principle.,2023/4/26,Institute of Computer Software,Nanjing University,50,回顾:Uniform Access,访问一致性原则客户应能以实现无关的方式访问模块的功能,不管这个功能是通过存储还是计算来实现的。Facilities managed by a module must be accessible to clients in the same way whether implemented by com

33、putation or storage.,2023/4/26,Institute of Computer Software,Nanjing University,51,Uniform Access,balance=list_of_deposits.total list_of_withdrawals.total,2023/4/26,Institute of Computer SoftwareNanjing University,52,Uniform access through feature call,To access a property of a point p1,the notatio

34、n 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 perform

35、ed.There is no difference for clients(except possibly in terms of performance).,2023/4/26,Institute of Computer Software,Nanjing University,53,类的使用,类的使用有两种形式允引(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_featur

36、e()or infix operatorsSingle target principle,2023/4/26,Institute of Computer Software,Nanjing University,54,Use 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 qendend,2023/4/26,Institute of

37、Computer Software,Nanjing University,55,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)endend,2023/4/26,Institute of Computer

38、 Software,Nanjing University,56,4.0,-2.0,p,(POINT),0.0,0.0,q,(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.scale(0

39、.5)endend,2023/4/26,Institute of Computer Software,Nanjing University,57,2.0,-1.0,p,(POINT),0.0,0.0,q,(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 wit

40、h Pascal,C,Ada:-Move(p,4.0,-2.0)p.scale(0.5)u:=p.distance(q)v:=p.xp:=qendend,2023/4/26,Institute of Computer Software,Nanjing University,58,2.0,-1.0,p,(POINT),0.0,0.0,q,(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:REALd

41、o-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)endend,2023/4/26,Institute of Computer Software,Nanjing University,59,2.0,-1.0,p,(POINT),0.0,0.0,q,(POINT),2023/4/26,Institute of Computer SoftwareNan

42、jing University,60,模块与类型的统一,从模块的观点:Set 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 instance

43、s of the class,viewed as a type.,Applying 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.,2023/4/26,Institute of Computer Software,Nanjing University,61,C,A,a1:A,The privileges of a

44、 client,2023/4/26,Institute of Computer Software,Nanjing University,62,Secret,Read-only,Read,restricted write,Full write,Applying abstraction principles,Beyond read access:full or restricted write,through exported procedures.Full write privileges:set_attribute procedure,e.g.set_temperature(u:REAL)is

45、-Set temperature value to u.dotemperature:=uensuretemperature_set:temperature=uendClient will use e.g.x.set_temperature(21.5).,2023/4/26,Institute of Computer Software,Nanjing University,63,Other uses of a setter procedure,set_temperature(u:REAL)is-Set temperature value to u.requirenot_under_minimum

46、:u=-273not_above_maximum:u=2000dotemperature:=uupdate_databaseensuretemperature_set:temperature=uend,2023/4/26,Institute of Computer Software,Nanjing University,64,Delphi/C#“properties”,Allowx.temperature:=21.5if there is a“setter”:private int temperature_internal;public int temperatureget return te

47、mperature_internal;set temperature_internal=value;/.Other instructions;.,2023/4/26,Institute of Computer Software,Nanjing University,65,Information hiding,2023/4/26,Institute of Computer Software,Nanjing University,66,class A featuref.g.feature NONEh.feature B,Cj.feature A,B,Ckend,In clients,with th

48、e declaration a1:A,we have:a1.f,a1.g:valid in 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 descendants,Information hiding(contd),Information hiding only applies

49、to use by clients,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,2023/4/26,Institute of Computer Software,Nanjing Universi

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

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

当前位置:首页 > 办公文档 > 文秘知识


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号