欢迎来到三一办公! | 帮助中心 三一办公31ppt.com(应用文档模板下载平台)
三一办公
全部分类
  • 办公文档>
  • PPT模板>
  • 建筑/施工/环境>
  • 毕业设计>
  • 工程图纸>
  • 教育教学>
  • 素材源码>
  • 生活休闲>
  • 临时分类>
  • ImageVerifierCode 换一换
    首页 三一办公 > 资源分类 > PPT文档下载  

    类和对象.ppt

    • 资源ID:4525842       资源大小:2.26MB        全文页数:94页
    • 资源格式: PPT        下载积分:5金币
    快捷下载 游客一键下载
    会员登录下载
    三方登录下载: 微信开放平台登录 QQ登录  
    下载资源需要5金币
    邮箱/手机:
    温馨提示:
    用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)
    支付方式: 支付宝    微信支付   
    验证码:   换一换

    加入VIP免费专享
     
    账号:
    密码:
    验证码:   换一换
      忘记密码?
        
    友情提示
    2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
    3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
    4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
    5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。

    类和对象.ppt

    类和对象,Class and Object,关于对象式语言,2023/4/26,Institute of Computer Software,Nanjing University,2,本课程并不系统讲授Eiffel语言但应学会“欣赏”Eiffel 语言(比较“纯”)它首先是一个对象式程序设计语言但强调从分析到设计到实现的平滑过渡,摘要,2023/4/26,Institute of Computer SoftwareNanjing University,3,三个世界:面向对象的建模面向对象原则类:对象程序静态的结构对象:运行时刻的结构对象创建:从类到对象对象引用:对象串起来,摘要,三个世界:面向对象的建模面向对象原则类:对象程序静态的结构对象:运行时刻的结构对象创建:从类到对象对象引用:对象串起来,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 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 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,Nanjing 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(),AcceptCourseOffering(),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(),BalanceinsterestYTDOwnerAccount_number Deposit()Withdraw()Transfer(),2023/4/26,Institute of Computer SoftwareNanjing University,15,封装,封装使得对象在抽象所界定的范围内保持相对的独立,从而保证对象设计和对象实施方法的可靠分离。设计者和使用者分离!封装可以防止由于程序模块之间的互相依赖性而导致程序“不稳定”不会因某个部分的修改而导致整个系统许多部分都发生改动。封装是相对的!不能也没有必要阻止编程人员去查看实施细节。,2023/4/26,Institute of Computer SoftwareNanjing University,16,封装 vs.继承,矛盾吗?No!封装性主要指的是对象的封装性,引入继承机制后,对象仍然是封装地很好的实体,其它对象与它通信只能发送消息。相似性:共享代码!继承:静态共享代码封装:动态共享代码,2023/4/26,Institute of Computer SoftwareNanjing University,17,模块化,传统的模块划分方法:要求组成的子程序(模块)有实际意义和明确的某个功能功能分解和结构分解OO的模块划分目标:生成一组有助于降低系统开发费用的模块,通过模块划分所得到的模块应该能够保证可以独立的生成和修改,每个模块的结构应该足够简单,2023/4/26,Institute of Computer SoftwareNanjing University,18,模块化,与抽象和封装密切相关模块的划分是以抽象出来的对象的性质和功能为依据的模块是封装得以实现的技术保障Booch:“模块化是反映一个被分解成一组相关联的松散结合模块组成系统的一个特性。”模块化以模块为基础,2023/4/26,Institute of Computer SoftwareNanjing University,19,模块与类型的统一,模块是软件分解的单元,是语法概念类型是某些动态对象的静态描述,是语义概念传统语言 模块与类型分离对象语言 模块与类型统一类型:类是抽象数据类型的实现模块:类是对象式程序的基本组成单元,基于类的面向对象的语言机制的强有力之处在于“类”统一了类型和模块,面向对象原则,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/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 Inheritance,2023/4/26,Institute of Computer SoftwareNanjing University,24,继承,继承 作为模块扩展机制,体现开闭原则,较好地支持软件的复用。继承 作为类型特化机制,体现分类方法,更合理地支持子类型实现。继承也是多态和动态绑定机制的基础。,2023/4/26,Institute of Computer SoftwareNanjing University,25,子类对父类的扩展与特化,继承表达的是一种分类演绎的认识方法“To program is to understand.”特化:包含范围变小,行为特殊化扩展:增加新的操作重定义:改变行为,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.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 modules open(developers perspective):One frequently overlooks aspects of the problem.,2023/4/26,Institute of 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 SoftwareNanjing 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,多态,多态性使指相同的操作或函数、过程可作用于多种类型的对象上并获得不同的结果。不同的对象,收到同一消息可以产生不同的结果,这种现象称为多态性。多态性允许每个对象以适合自身的方式去响应共同的消息多态性增强了软件的灵活性和重用性。,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 Circle();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,多态和动态绑定,通过类继承机制实现子类型,允许子类重定义父类的操作。这样父类型变量(对象引用)及其上的的操作必然是多态的。这就在实现上要求动态绑定机制,2023/4/26,Institute of Computer SoftwareNanjing University,36,动态绑定,绑定:就是建立method call和method body(方法本体)的关联。静态绑定:指绑定动作发生于程序执行前(由编译器和连接器完成)。动态绑定:指绑定动作将在执行期间才根据对象类型而进行,不同的对象类型将作出不同的编译结果。具有灵活性,但效率有一点损失。找到最合适的method body,摘要,2023/4/26,Institute of Computer SoftwareNanjing University,37,三个世界:面向对象的建模面向对象原则类:对象程序静态的结构对象:运行时刻的结构对象创建:从类到对象对象引用:对象串起来,类:对象程序静态的结构,类是对象式软件系统的基本组成单元类的构成类的使用信息隐蔽设施模块与类型,2023/4/26,Institute of Computer Software,Nanjing University,38,Class,一般说类是抽象数据类型的实现抽象数据类型乃是一种规约;类是OOPL实现这种类型的设施;但是Meyer说:“A class is an abstract data type equipped 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-Increase 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 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,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 is-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.类的实例在运行时刻产生,他们是对象。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 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 call)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 result,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,Command,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.Feature 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 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 computation 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 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 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).,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_feature()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 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 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.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 with 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: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)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 SoftwareNanjing 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 instances 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 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-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: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 temperature_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 the 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 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 University,67,DVD播放机,摘要,2023/4/26,Institute of Computer SoftwareNanjing University,68,三个世界:面向对象的建模面向对象原则类:对象程序静态的结构对象:运行时刻的结构对象创建:从类到对象对象引用:对象串起来,对象:系统的运行结构,对象:A run-time instance of some class.某对象O是某类C的(直接)实例O包含为C中定义的属性(数据成员)当前状态(O的fields)运行规律类定义的行为面向对象的软件系统运行时由一组对象构成。对象是对问题域对象,并进而对现实对象的实现,三种对象概念上的一致性与差异性,

    注意事项

    本文(类和对象.ppt)为本站会员(文库蛋蛋多)主动上传,三一办公仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知三一办公(点击联系客服),我们立即给予删除!

    温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载不扣分。




    备案号:宁ICP备20000045号-2

    经营许可证:宁B2-20210002

    宁公网安备 64010402000987号

    三一办公
    收起
    展开