




全文预览已结束
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
南 京 理 工 大 学 紫 金 学 院 毕业设计 论文 外文资料翻译 系 计算机 系 专 业 计算机科学与技术 姓 名 学 号 外 文 出 处 用外文写 Ghosh D Java Object oriented programming J IEEE Transactions on Software Engineering 2009 13 3 42 45 附 件 外文资料翻译译文 外文原文 指导教师评语 签名 年 月 日 注 请将该封面与附件装订成册 附件 外文资料翻译译文 Java 的面向对象编程 面向对象编程和它的关键技术 继承和多态 性 软件的重用可以节省程序开发时间 它鼓励重复使用已经调试好的高质量的软件 从而减少系统运 行后可能出现的问题 这些都是令人振奋的可能性 多态性允许我们用统一的风格编写程序 来处理 多种已存在的类和特定的相关类 利用多态性我们可以方便地向系统中添加新的功能 继承和多态对于 解决软件的复杂性是一种有效可行的技术 当创建一个新的类时 而不用完整的写出新的实例变量和实 例方法 程序员会指定新的类继承已定义的超类的实例变量和实例方法 这个新的类被称为一个子类 每个子类本身将来亦可有新的子类 而其本身将成为父类 一个类的直接父类就是该类所直接继承的类 通过关键字 extends 继承 一个间接超类是通过从两级或更多级以上的类继承而来的 例如 从类 JApplet 包 javax swing 中 扩展来的类 Applet 包 java applet 一个类单一的从一个父类继承 而来 Java 不支持多重继承 而 C 可以 但它支持接口的概念 接口可以使 Java 实现许多通过 多重继承才能实现的优点而没有关联的问题 我们将在本章讨论的接口的详细内容 我们会给出创建和 使用接口的一般规律和具体实例 一个子类通常添加自己的实例变量和自己的实例方法 因此子类通常 比父类大 一个子类比它的父类更具体并且代表一组更小 更专业的对象 通过单一继承 子类在开 始时拥有父类的所有特性 继承性真正的力量在于它可以在定义子类时增加或取代从超类中继承来的特 征 每个子类对象也是该类的父类的对象 例如 每一个我们所定义的小程序被认为是类 JApplet 的 对象 此外 因为 Japplet 继承了 Applet 每一个我们所定义的小程序同时也被认为是一个 Applet 的对象 当开发 applets 时 这些信息是至关重要的 因为一个小程序容器只有当它是一个 Applet 才可以执行一个程序 虽然子类对象始终可以作为它的父类的一种来看待 父类对象却不被认为是其子 类类型的对象 我们将利用这种 子类对象是父类对象 的关系来执行一些强大的操作 例如 绘图 程序可以显示一系列图形 如果所有的图形类型都直接或间接地继 承同一超类 绘图程序就可以将所 有图形存储在一个超类对象数组或其他数据结构中 正如我们将要在这一章中看到的 这种处理单一类 型的一系列的对象的能力是推动面向对象程序发展的重要推动力 我们添加一个新的成员访问的一种控 制形式 protected 访问 由同一个包中子类和其他类的方法组成的父类可以访问受保护的父类的成 员 开发软件的实践经验表明 处理的代码的重要部分涉及密切相关的案例 因为设计人员和程序员 十分专注于特殊案例 所以很难在这种系统中看到 大局 面向对象编程提供 透过树木见森林 的几种方法 程序员和设计人员专注于系统中对象的共性而不是特定实例 这种方法叫做抽象 如果 一个程序性方案有许多密切相关的案例 那么就会常用到 switch 交换结构或嵌套的 if else 结构从而 区分众多的案例并提供独立处理各个案例的逻辑 我们下面将学习如何使用继承性和多态性以更简单的逻 辑来代替 switch 结构 我们区分 is a 关系和 has a 的关系 is a 就是继承 在 是 关系中 子类类型的对象也可以被看作是它的父类型的对象处理 有 是一种构成 在 有 关系 中 一个类对象有一个或多个其它类的对象作为成员 例如 车有方向盘 子类的方法可能需要确切 访问它的父类的实例变量和方法 在 Java 中 软件工程的一个至关重要的方面就是子类不能访问其父 类的私有成员 如果子类可以访问父类的私有成员 这就违背了父类的信息隐蔽原则 然而 一个子 类可以访问它的父类的 public 成员和 protected 成员 如果子类和父类在同一个包中 子类也可以 使用它的程序包访问父类成员 如果超类不允许通过继承产生的子类访问它的某些成员 则需要用 private 声明成员 子类只可以通过公有继承 受保护继承和程序包借助父类提供的继承到子类中的方 法声明对父类中私有成员的改变 继承会带来的问题是 子类会将一些没必要继承或不该继承方法也继 承过来 类的编写者应确保由类提供的功能对未来的子类是适用的 即使父类的方法对子类适用 子类 也可以使该方法来完成特定于子类的方式的任务 在这种情况下 子类可以覆盖超类的方法 以提供 一种合适的实现 也许最令人兴奋的是一个新类可以从丰富的类库中继承 比如 Java API 提供许多类 一些组织开发自己的类库的同时还具有可以利用现有的全球其他库的优势 总有一天 大多数的软件 可以通过可重用的标准化的组件来构建 就像现在制造硬件一样 这将有助于面对开发我们将来可能需 要的更加强大的软件的挑战 我们常常会遇到这样一个问题 一个类的对象同时还 是 另一个类的 对象 矩形肯定是一个四边形 如同正方形 平行四边形和梯形 因此 Rectangle 类可以说是从 Quadrilateral 类继承而来的 在这种情况下 Quadrilateral 类是父类 Rectangle 类是一个子类 矩形是特殊的四边形 但却不能说四边形就是矩形 四边形可能是平行四边形 继承通常能够产生比 其父类具有更多功能的子类 所以父类和子类的概念很容易引起混淆 然而 我们还有另种一更好的 方法来区分这两个概念 因为每个子类对象也 是 其父类的对象 而一个父类又可以有很多子类 那么由父类所代表的一组对象通常比由该对象的父类的任何子类所代表的要大得多 例如 Vehicle 超 类包含了所有的交通工具 如汽车 卡车 船 自行车等 而 Car 子类仅表示交通工具 Vehicle 中 的一个小子集 继承关系可以用树型层次结构来表示 一个父类与它的子类的构成层次关系 一个类肯 定可以独自存在 但当一个类是运用了继承的机理时 这个类要么成为一个提供属性和行为的父类 要 么成为一个继承这些属性和行为的子类 通常情况下 一个类既是父类也是子类 一个子类的对象可以 被当作其父类的对象来处理 这使得可能会有一些有趣的操作 例如 尽管有从一个特定的父类派生 出的众多子类的对象彼此之间可能会有很大的不同的事实 但只要我们将他们当作父类的对象来看 我 们仍可以创建一个引用的数组给他们 但反之则不然 一个父类对象不能被当成一个子类对象 例如 图形 Shape 不一定是圆 Circle 尽管子类的对象也 是 父类的对象 子类类型和父类类型却 是不相同的 子类对象可以被视为父类的对象 这是合理的 因为子类含有和每个超类成员相对应的 成员 但也应认识到 子类通常拥有比父类更多的成员 但 Java 不允许逆向赋值 因为如果把超类 对象赋给子类的引用 就会使子类中额外的子类成员没有定义 子类对象的引用可以被隐式地转换为超 类对象的引用 因为根据继承性 子类对象 是 超类对象 有四种可能的方法来使父类的对象和子 类的对象与父类的引用和子类的引用相匹配 1 父类对象与可以直接用父类的引用 2 子类对象与 可以直接用子类的引用 3 子类的对象用父类的引用是安全的 因为子类对象同样也是其父类的对象 但这样的代码只能引用父类的成员 如果该代码通过父类的引用来访问子类所特有的成员 编译器就会 报告一个语法错误 4 如果用子类来引用父类的对象 将报告一个语法错误 尽管将子类的对象看 成是父类的对象会带来很大的便利 而且可以通过这些父类的引用操纵这些对象来实现 但这会引发一 个问题 在一个工资管理系统 例如 我们希望能够通过一组员工数组来计算每个人的周薪 但是直 觉告诉人们 使用父类的引用将使程序只调用父类的工资计算程序 如果父类中确实有这样一个程序的 话 我们需要一种方式仅通过使用父类的引用来正确的调用对每个对象 无论是父类对象或子类对象 相对应的工资计算程序 其实 这正是当我们考虑了多态性和动态链接后 Java 所呈现的并将在此章节 中讨论的内容 我们可以使用继承来定制现有的软件 当我们使用继承来从现有类创建一个新类时 新 类将继承的原有类的属性和行为 此时我们可以添加属性和行为或重载父类的行为来定制能满足我们需 要的类 很难让学生意识到设计师和工业的大型软件项目的实施者所面临的问题 做过这类项目的人都 会说提高软件开发效率的最好方法是鼓励对软件的再利用 一般来说 面向对象的编程 像 Java 就 可以实现 实质和有用的类库使得通过及继承而实现的软件重用实现利益最大化 人们对 Java 类库的 兴趣随着 Java 的发展而增加 正如独立软件厂商生产的压缩套装的软件伴随着个人电脑的问世而出现 爆炸性的增长一样 Java 类库的制造和销售也将呈现这样一个趋势 应用程序设计人员将使用这些类 库来构建他们的应用程序 而类库的设计者会因为将其开发的类库与应用程序打包在一起而得到收益 在不久的将来 随着在大量领域的应用 Java 的类库将会起到至关重要的作用并得到极大的发展 附 件 外文原文 复印件 Java Object oriented programming Object oriented programming and its key component technologies as inheritance and polymorphism Software reusability saves time in program development It encourages reuse of proven and debugged high quality software thus reducing problems after a system becomes operational These are exciting possibilities Polymorphism enables us to write programs in a general fashion to handle a wide variety of existing and yet to be specified related classes Polymorphism makes it easy to add new capabilities to a system Inheritance and polym orphism are effective techniques for dealing with software complexity When creating a new class instead of writing completely new instance variables and instance methods the programmer can designate that the new class is to inherit the instance variables and instance methods of a previously defined superclass The new class is referred to as a subclass Each subclass itself becomes a candidate to be a superclass for some future subclass The direct superclass of a class is the superclass from which the class explicitly inherits via the keyword extends An indirect superclass is inherited from two or more levels up the class hierarchy For example class JApplet package javax swing extends class Applet package java applet With single inheritance a class is derived from one superclass Java does not support multiple inheritance as C does but it does support the notion of interfaces interfaces help Java achieve many of the advantages of multiple inheritance without the associated problems We will discuss the details of interfaces in this chapter We consider both general principles and a detailed specific example of creating and using interfaces A subclass normally adds instance variables and instance methods of its own so a subclass is generally larger than its superclass A subclass is more specific than its superclass and represents a smaller more specialized group of objects With single inheritance the subclass starts out essentially the same as the superclass The real strength of inheritance comes from the ability to define in the subclass additions to or replacements for the features inherited from the superclass Every subclass object is also an object of that class s superclass For example every applet we have defined is considered to be an object of class JApplet Also because JApplet extends Applet every applet we have defined is considered to be an Applet This information is critical when developing applets because an applet container can execute a program only if it is an Applet Although a subclass object always can be treated as one of its superclass types superclass objects are not considered to be objects of their subclass types We will take advantage of this subclass object is a superclass object relationship to perform some powerful manipulations For example a drawing application can maintain a list of shapes to display If all the shape types extend the same superclass directly or indirectly the drawing program can store all the shapes in an array or other data structure of superclass objects As we will see in this chapter this ability to process a set of objects as a single type is a key thrust of object oriented programming We add a new form of member access control in this chapter namely protected access Subclass methods and methods of other classes in the same package as the superclass can access protected superclass members Experience in building software systems indicates that significant portions of the code deal with closely related special cases It becomes difficult in such systems to see the big picture because the designer and the programmer become preoccupied with the special cases Object oriented programming provides several ways of seeing the forest through the trees The programmer and designer concentrate on the big picture the commonality among objects in the system rather than the special cases This process is called abstraction If a procedural program has many closely related special cases then it is common to see switch structures or nested if else structures that distinguish among the special cases and provide the processing logic to deal with each case individually We will show how to use inheritance and polymorphism to replace such switch logic with much simpler logic We distinguish between the is a relationship and the has a relationship Is a is inheritance In an is a relationship an object of a subclass type may also be treated as an object of its superclass type Has a is composition In a has a relationship a class object has one or more objects of other classes as members For example a car has a steering wheel A subclass s methods might need to access certain of its superclass s instance variables and methods A crucial aspect of software engineering in Java is that a subclass cannot access the private members of its superclass If a subclass could access the superclass s private members this would violate information hiding in the superclass However a subclass can access the public and protected members of its superclass A subclass also can use the package access members of its superclass if the subclass and superclass are in the same package Superclass members that should not be accessible to a subclass via inheritance are declared private in the superclass A subclass can effect state changes in superclass private members only through public protected and package access methods provided in the superclass and inherited into the subclass A problem with inheritance is that a subclass can inherit methods that it does not need or should not have It is the class designer s responsibility to ensure that the capabilities provided by a class are appropriate for future subclasses Even when the superclass methods are appropriate for the subclasses it is common for a subclass to require the method to perform a task in a manner that is specific to the subclass In such cases the superclass method can be overridden redefined in the subclass with an appropriate implementation Perhaps most exciting is the notion that new classes can inherit from abundant class libraries such as those provided with the Java API Organizations develop their own class libraries and can take advantage of other libraries available worldwide Someday most software might be constructed from standardized reusable components just as hardware is often constructed today This will help meet the challenges of developing the ever more powerful software we will need in the future Often an object of one class is an object of another class as well A rectangle certainly is a quadrilateral as are squares parallelograms and trapezoids Thus class Rectangle can be said to inherit from class Quadrilateral In this context class Quadrilateral is a superclass and class Rectangle is a subclass A rectangle is a specific type of quadrilateral but it is incorrect to claim that a quadrilateral is a rectangle the quadrilateral could be a parallelogram Inheritance normally produces subclasses with more features than their superclasses so the terms superclass and subclass can be confusing There is another way however to view these terms that makes perfectly good sense Because every subclass object is an object of its superclass and because one superclass can have many subclasses the set of objects represented by a superclass is normally larger than the set of objects represented by any of that superclass s subclasses For example the superclass Vehicle represents in a generic manner all vehicles such as cars trucks boats bicycles and so on However subclass Car represents only a small subset of all the Vehicles in the world Inheritance relationships form tree like hierarchical structures A superclass exists in a hierarchical relationship with its subclasses A class can certainly exist by itself but it is when a class is used with the mechanism of inheritance that the class becomes either a superclass that supplies attributes and behaviors to other classes or a subclass that inherits those attributes and behaviors Frequently one class is both a subclass and a superclass An object of a subclass can be treated as an object of its superclass This makes possible some interesting manipulations For example despite the fact that objects of a variety of classes derived from a particular superclass might be quite different from one another we can create an array of references to them as long as we treat them as superclass objects But the reverse is not true A superclass object cannot always be treated a subclass object For example a Shape is not always a Circle Despite the fact that a subclass object also is a superclass object the subclass type and the superclass type are different Subclass objects can be treated as superclass objects This makes sense because the subclass has members corresponding to each of the superclass members remember that the subclass normally has more members than the superclass has Assignment in the other direction is not allowed because assigning a superclass object to a subclass reference would leave the additional subclass members undefined A reference to a subclass object could be implicitly converted into a reference to a superclass object because a subclass object is a superclass object through inheritance There are four possible ways to mix and match superclass references and subclass references with superclass objects and subclass objects 1 Referring to a superclass object with a superclass reference is straightforward 2 Referring to a subclass object with a subclass reference is straightforward 3 Referring to a subclass object with a superclass reference is safe because the subclass object is an object of its superclass as well Such code can refer only to superclass members If this code refers to subclass only members through the superclass reference the compiler will report a syntax error 4 Referring to a superclass object with a subclass reference is a syntax error As convenient as it might be to treat subclass objects as superclass objects and to do this by manipulating all these objects with superclass references there appears to be a problem In a payroll system for example we would like to be able to walk through an array of employees and calculate the weekly pay for each person But intuition suggests that using superclass references would enable the program to call only the superclass payroll calculation routine if indeed there is such a routine in the superclass We need a way to invoke the proper payroll calculation routine for each object whether it is a super
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025版金融投资合伙经营退伙协议书
- 2025年度石油钻井平台电工外包及设备检修服务合同
- 二零二五年度禁止放牧技术培训与服务合同
- 2025年度城市配送大货车运输服务合同模板
- 二零二五年度跨区域建筑垃圾集中运输及资源化利用服务协议
- 2025版城市公共供水服务合同模板
- 2025版纺织品跨境电商代理销售合同
- 二零二五年度高端家具框架销售合同范本
- 二零二五年度科研机构间实验场地租赁合同
- 二零二五年度国际贸易融资借款合同
- 抖音蓝v合同协议
- 2025年黑龙江省大庆市中考一模语文试题(含答案)
- DB11-T 2398-2025 水利工程巡视检查作业规范
- 宫颈癌考试题及答案
- 2025-2030全球及中国废机油市场发展趋势及投资运行状况监测研究报告(-版)
- 危险废物暂存库管理制度
- 屠宰场建设实施方案
- 2025年德邦快递运输合同模板
- YY/T 1926.2-2024脊柱植入器械临床前力学性能评价和特殊要求第2部分:椎间融合器
- 酒驾复检申请书
- 工程竣工验收申请表
评论
0/150
提交评论