




已阅读5页,还剩81页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Class and Object,马晓星 南京大学计算机软件研究所 南京大学计算机科学与技术系,2019/11/16,Institute of Computer Software Nanjing University,2,关于对象式语言,本课程并不系统讲授Eiffel语言 但应学会“欣赏” Eiffel 语言 (比较“纯”) 它首先是一个对象式程序设计语言 但强调从分析到设计到实现的平滑过渡 本课程主要讨论基于“类”的面向对象语言,但另有一些语言并不基于“类”概念。,2019/11/16,Institute of Computer Software Nanjing University,3,Outline,Three worlds: Object-Oriented Modeling Class: the static structure Object: the run-time structure Object creation: from classes to objects Object reference: linking up objects,2019/11/16,Institute of Computer Software Nanjing University,4,Outline,Three worlds: Object-Oriented Modeling Class: the static structure Object: the run-time structure Object creation: from classes to objects Object reference: linking up objects,2019/11/16,Institute of Computer Software Nanjing University,5,Three worlds,客观世界 DVD播放机 What is reality? 复杂性 认识的主观性 问题世界 抽象!Tell me not what you are but what you have A model of a subset of the reality DVD播放机 计算机(软件)世界 A model of a model of a subset of the reality,2019/11/16,Institute of Computer Software Nanjing University,6,现实世界,问题世界,软件世界,Reality,Abstract,2019/11/16,Institute of Computer Software Nanjing University,7,Outline,Three worlds: Object-Oriented Modeling Class: the static structure Object: the run-time structure Object creation: from classes to objects Object reference: linking up objects,2019/11/16,Institute of Computer Software Nanjing University,8,Class: the static structure,类是对象式软件系统的基本组成单元 类的构成 类的使用 信息隐蔽设施 模块与类型 类如何构成系统? 其它,2019/11/16,Institute of Computer Software Nanjing University,9,Class,“A class is an abstract data type equipped with a possibly partial implementation.” Deferred and effective class But why?,10,A very deferred class,deferred class COUNTER feature item: INTEGER is deferred end - Counter value up is - Increase item by 1. deferred ensure item = old item + 1 end down is - Decrease item by 1. deferred ensure item = old item 1 end invariant item = 0 end,2019/11/16,Institute of Computer Software Nanjing University,11,Class,Mold and instance 类自身也能被当作对象么,2019/11/16,Institute of Computer Software Nanjing University,12,类的构成,类名 继承关系 成员 术语 分类,13,Abstract data type POINT,x: POINT REAL y: POINT REAL : POINT REAL : POINT REAL Class POINT: Choose a representation (polar, cartesian) In polar representation, and are attributes, x and y are routines.,y,x,14,A simple class,class POINT feature x, y: REAL - Point cartesian coordinates move (a, b: REAL) is - Move by a horizontally and by b vertically. do x := x + a y := y + b end scale (factor: REAL) is - Scale by factor. do x := factor * x y := factor * y end,15,Class POINT (contd),distance (p: POINT): REAL is - Distance to p do Result := sqrt (x p.x)2 + (y p.y)2) end ro: REAL is - Distance to origin (0, 0) do Result := sqrt (x2 + y2) end theta: REAL is - Angle to horizontal axis do end end,16,Terminology,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.,17,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,18,Feature categories by role,Command,Query,Feature,Procedure,Attribute,Function,No result,Returns result,Computation,Memory,19,Feature categories by implementation,Procedure,Attribute,Function,Routine,Returns result,No result,Feature,Memory,Computation,20,Feature categories,Command,Query,Feature,Procedure,Attribute,Function,No result,Returns result,Computation,Memory,Routine,Returns result,No result,Feature,Memory,Computation,21,Uniform Access,balance = list_of_deposits.total list_of_withdrawals.total,list_of_deposits,list_of_withdrawals,balance,list_of_deposits,list_of_withdrawals,(A2),(A1),22,The Principle of Uniform Access,Facilities managed by a module must be accessible to clients in the same way whether implemented by computation or storage.,23,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.x which 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).,2019/11/16,Institute of Computer Software Nanjing University,24,关于C+/Java的静态成员,有力的设施 但并非面向对象计算所必须? 不修改静态数据成员的 修改静态数据成员的 破坏面向对象的“纯粹性” single target principle (see below ) 考虑类作为对象?,2019/11/16,Institute of Computer Software Nanjing University,25,类的使用,类的使用有两种形式 允引 (class A is a client of class B) 继承 (class A is a descendant of class B) Client and supplier a:S in C Feature call a.some_feature or a.some_feature() or infix operators Single target principle,26,Use of the class in a client (1/5),class GRAPHICS feature p, q: POINT - Graphic points some_routine is - Use p and q. local u, v: REAL do - Creation instructions create p create q end end,27,Use of the class in a client (2/5),class GRAPHICS feature p, q: POINT - Graphic points some_routine is - Use p and q. local u, v: REAL do - Creation instructions create p create q p.move (4.0, -2.0) - Compare with Pascal, C, Ada: - Move (p, 4.0, -2.0) end end,4.0,-2.0,p,(POINT),0.0,0.0,q,(POINT),28,Use of the class in a client (3/5),class GRAPHICS feature p, q: POINT - Graphic points some_routine is - Use p and q. local u, v: REAL do - Creation instructions create p create q p.move (4.0, -2.0) - Compare with Pascal, C, Ada: - Move (p, 4.0, -2.0) p.scale (0.5) end end,2.0,-1.0,p,(POINT),0.0,0.0,q,(POINT),29,Use of the class in a client (4/5),class GRAPHICS feature p, q: POINT - Graphic points some_routine is - Use p and q. local u, v: REAL do - Creation instructions create p create q p.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.x p := q end end,2.0,-1.0,p,(POINT),0.0,0.0,q,(POINT),30,Use of the class in a client (5/5),class GRAPHICS feature p, q: POINT - Graphic points some_routine is - Use p and q. local u, v: REAL do - Creation instructions create p create q p.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.x p := q p.scale (-3.0) end end,2.0,-1.0,p,(POINT),0.0,0.0,q,(POINT),2019/11/16,Institute of Computer Software Nanjing University,31,类的信息隐蔽设施,回忆 C+和Java的信息隐蔽设施 看Eiffel的设计 细粒度的设计,32,Applying abstraction principles,Privileges of a client C of a class A on an attribute attrib: Read access if attribute is exported. Assuming a1: A Then a1.attrib is an expression. An assignment such as a1.attrib := a2 is syntactically illegal! (You cannot assign a value to an expression, e.g. x + y.),C,A,a1: A,33,The privileges of a client,Secret,Read-only,Read, restricted write,Full write,34,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. do temperature := u ensure temperature_set: temperature = u end Client will use e.g. x.set_temperature (21.5).,35,Other uses of a setter procedure,set_temperature (u: REAL) is - Set temperature value to u. require not_under_minimum: u = -273 not_above_maximum: u = 2000 do temperature := u update_database ensure temperature_set: temperature = u end,36,Delphi/C# “properties”,Allow x.temperature := 21.5 if there is a “setter”: private int temperature_internal; public int temperature get return temperature_internal; set temperature_internal = value; /. Other instructions; . ,37,Information hiding,class A feature f . g . feature NONE h . feature B, C j . feature A, B, C k end,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,38,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 A feature NONE h is - Does something. do . end feature f is - Use h. do . h end end,2019/11/16,Institute of Computer Software Nanjing University,39,关于子类访问权限,C+ Java Eiffel and SmallTalk,2019/11/16,Institute of Computer Software Nanjing University,40,模块与类型的统一,模块是软件分解的单元,是语法概念 类型是某些动态对象的静态描述,是语义概念 传统语言模块类型分离 C语言的函数指针? 对象语言 统一模块与类型,2019/11/16,Institute of Computer Software Nanjing University,41,The module-type merge,A class is both: A module A type From the module viewpoint: Set of available services (“features”). From the type viewpoint: Description of set of possible run-time objects (its instances).,2019/11/16,Institute of Computer Software Nanjing University,42,模块与类型的统一,结合继承,威力更增 Extension of the modular Specialization of the type But How? 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.,2019/11/16,Institute of Computer Software Nanjing University,43,类如何构成系统?,允引,允引,再允引 第一驱动问题 Root class and its creation procedure name “上帝之手”不应存在于对象软件正文之中。 非集中式的架构 避免直接指定动作顺序,2019/11/16,Institute of Computer Software Nanjing University,44,其它,“纯”面向对象的效率问题 基本类型 对象式程序的风格 小方法 编译优化 效率不那么critical,2019/11/16,Institute of Computer Software Nanjing University,45,Outline,Three worlds: Object-Oriented Modeling Class: the static structure Object: the run-time structure Object creation: from classes to objects Object reference: linking up objects,2019/11/16,Institute of Computer Software Nanjing University,46,例,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+?,2019/11/16,Institute of Computer Software Nanjing University,47,对象系统的运行结构,对象:A run-time instance of some class. 某对象O是某类C的(直接)实例 O包含为C中定义的属性(数据成员) 当前状态 (O的fields) 运行规律 类定义的行为 面向对象的软件系统运行时由一组对象构成。 对象是对问题域对象,并进而对现实对象的实现, 三种对象概念上的一致性与差异性,2019/11/16,Institute of Computer Software Nanjing University,48,对象结构,回头看Point类 和 point 对象 简单类型的域与引用类型的域,2019/11/16,Institute of Computer Software Nanjing University,49,2019/11/16,Institute of Computer Software Nanjing University,50,2019/11/16,Institute of Computer Software Nanjing University,51,2019/11/16,Institute of Computer Software Nanjing University,52,2019/11/16,Institute of Computer Software Nanjing University,53,对象,对象引用: A reference is a run-time value which is either void or attached. If attached, a reference identifies a single object (be attached to this object) 对象的identity 不同对象可有完全一样的域 一个对象的域可随系统执行而变化,2019/11/16,Institute of Computer Software Nanjing University,54,对象,引用声明 class C feature . End x:C 看前面点的例子 Self reference is possible,2019/11/16,Institute of Computer Software Nanjing University,55,运行时刻结构,2019/11/16,Institute of Computer Software Nanjing University,56,Outline,Three worlds: Object-Oriented Modeling Class: the static structure Object: the run-time structure Object creation: from classes to objects Object reference: linking up objects,2019/11/16,Institute of Computer Software Nanjing University,57,Object creation,对象按需创建,显式创建。 传统技术往往基于栈分配实体 运行时刻对象动态结构多变,而难以根据程序文本预测 Eiffel 的对象创建 基本创建 基本创建 初始化feature Creation procedures,58,Creating 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 declared 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.,59,Specific creation procedures,class POINT create make_cartesian, make_polar feature - Initialization make_cartesian (a, b: REAL) is - Initialize to abscissa a, ordinate b. do x := a y := b end make_polar . feature . The rest as before .,60,If there is a creation clause,Creation instructions must be “creation calls”, such as create my_point.make_polar (1, Pi / 2),61,If there is no creation clause,An absent creation clause, as in class POINT - No creation clause feature The rest as before end is understood as one that would only list default_create, as if it had been written class POINT create default_create feature The rest as before end Procedure default_create is defined in ANY as doing nothing; any class can redefine it to provide proper default initializations.,62,Associated convention,The notation create x is understood (if permitted) as an abbreviation for create x.default_create,63,To allow both forms,To make both forms valid: create my_point as well as create my_point.make_polar (1, Pi / 2) it suffices to make default_create (redefined or not) one of the creation procedures: class POINT create make_cartesian, make_polar, default_create feature . The rest as before .,64,To prohibit instantiating a class,class NOT_CREATABLE create - Nothing here! feature . The rest as before . end,2019/11/16,Institute of Computer Software Nanjing University,65,Outline,Three worlds: Object-Oriented Modeling Class: the static structure Object: the run-time structure Object creation: from classes to objects Object reference: linking up objects,66,The dynamic model,States of a reference: Operations on references: create p p := q p := Void if p = Void then .,VOID,ATTACHED,create p p := q (where q is attached),p := Void p := q (where q is void),p,ATTACHED,p,VOID,2019/11/16,Institute of Computer Software Nanjing University,67,Object reference,回顾C+和Java的相应机制 Void Reference, clone, copy and compare 对象引用的问题 引用类型与扩展类型,68,Forms 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 is created): e.copy (a),69,Shallow and deep cloning,Initial situation: Result of: b := a c := clone (a) d := deep_clone (a),“Almaviva”,name,landlord,loved_one,a,O1,“Figaro”,O2,“Susanna”,O3,b,“Almaviva”,O4,c,“Almaviva”,name,landlord,loved_one,O5,“Figaro”,O6,“Susanna”,O7,d,70,A related mechanism: Persistence,a.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).,2019/11/16,Institute of Computer Software Nanjing University,71,Object references,对象引用的问题 Dynamic Aliasing,NO SURPRISE - Assume that here P (y) holds x := y C (x) /not involve y textually - Then here P (y) still holds.,2019/11/16,Institute of Computer Software Nanjing University,72,引用类型与扩展类型,73,Types,Reference types; value of an entity is a reference. Example: b: POINT Expanded types; value of an entity is an object. Example: d: expanded POINT,b,(POINT),d,(POINT),74,Expanded classes,A class may also be declared as expanded class C . The rest as usual . Then you can declare: a: C with the same effect as b: expanded C in the earlier syntax (still permitted, with same meaning).,75,Subobjects,Expanded classe
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 抵押车买卖合同7篇
- 北京房屋个人装修合同2篇
- 合同协议-装饰公司设计合同2篇
- 新解读《GB-T 31095-2014地震情况下的电梯要求》
- 新解读《GB-T 31149-2014汽车物流服务评价指标》
- 新解读《GB-T 31209-2014绿色制造 低温冷风切削 技术要求》
- 工地吊篮销售合同范本
- 房屋代理租赁合同范本
- 商业用地出让合同范本
- 公伤补偿合同范本
- 常见肛周疾病的治疗及护理
- 护蕾行动法律课件
- 福建水投集团检测科技有限公司招聘笔试题库2025
- 高层建筑无人机巡检系统施工方案
- 乡村春晚活动方案
- CJ/T 516-2017生活垃圾除臭剂技术要求
- 大头儿子测试题及答案
- T/CGCC 17-2018商业信誉评价体系
- 商场保洁服务应急预案
- 低空经济培训项目工程方案
- 外贸英语专业课件
评论
0/150
提交评论