理工类毕业论文—英文文献翻译—Javadetailedanalysisofabstractclassesandinterfacesdistinction_第1页
理工类毕业论文—英文文献翻译—Javadetailedanalysisofabstractclassesandinterfacesdistinction_第2页
理工类毕业论文—英文文献翻译—Javadetailedanalysisofabstractclassesandinterfacesdistinction_第3页
已阅读5页,还剩10页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、Java detailed analysis of abstract classes and interfaces distinctionIn the Java language, the abstract class and the interface is to support the definition of the abstract category two mechanisms. It is precisely because the existence of these two mechanisms, it has given a powerful object-oriented

2、 Java capability. Abstract class and the interface between the abstract category in the definition of support is very similar, and even replace each other, many developers in the abstract definition of the abstract class, and the choice of interface is relatively arbitrary. In fact, between the two

3、there is still great difference,even for their choice reflects the nature of the problem areas for understanding, the understanding of the design intent is correct and reasonable. In this paper, they will carry out the difference between a dissection, and attempt to provide developers with a choice

4、between the two, basis.Understand abstract class:Abstract class and interface in the Java language are used for abstract classes (This article is not in the abstract category come from the abstract class translation, it said that the body is an abstract, and abstract class for the Java language is u

5、sed to define the abstract category a method to distinguish the attention of readers) definition, then what is an abstract class, the use of an abstract class can bring us any good ?In the object-oriented concept, we know that all objects are described by type, but the contrary is not the case. Not

6、all categories are used to describe the object, if a class does not contain enough information to describe a specific target, such a class is an abstract class. An abstract class is often used to our characterization of the problem areas in the analysis, design drawn abstraction is a series of looks

7、 different, but essentially the same concept of the abstract concrete. For example: If we have a graphics editing software development, we will find that there are areas of circular, triangular this specific concept, they are different, but they also belong to the shape of such a concept, the concep

8、t of shape is not in the problem areas exist, it is an abstract concept. It is precisely because the concept of abstract no counterpart in the field of the specific concept, used to describe abstract concept of an abstract class can not be the case.In object-oriented fields, mainly used for abstract

9、 class type hide. We can construct a fixed behavior of a group of abstract description, but this group is able to conduct arbitrary possible to achieve the specific way. This is the abstract description of the abstract category, and this group of arbitrary possible a concrete realization of the perf

10、ormance for all possible derived class. Modules can operate in an abstract. Because the module relies on the abstract of a fixed, it can be modified; At the same time, from the abstract of this derivative, may also expand this module function. Readers familiar with the OCP will be aware that in orde

11、r to achieve the object-oriented design one of the most core principles OCP (Open-Closed Principle), an abstract class is one of the key.Syntax definition from the perspective abstract class and interface:In the syntax level, the Java language interface and the abstract class is given a different de

12、finition, a definition below to the abstract class called Demo as an example to illustrate the difference.Use the abstract class defined Demo abstract class in the following manner:abstract class Demoabstract void method1(); abstract void method2();Use the interface Demo abstract class defined in th

13、e following manner: interface Demovoid method1(); void method2();In the abstract class methods, Demo can have their own data members can be members of the non-abstract approach, and the way in the implementation of the interface, Demo can only have static data can not be amended members (that is, mu

14、st be static final , but generally not in the interface definition data members), all members of the methods are abstract. In a sense, the interface is a special form of abstract class.From the point of view of programming, the abstract class and the interface can be used to achieve the "design

15、 by contract" thinking. However, in the use of concrete above or are there some distinction.First, the abstract class in the Java language that is an inheritance, a class can only be used once Inheritance (because Java does not support multiple inheritance - to the Note). However, a class can i

16、mplement multiple interface. Perhaps this is the designers of the Java language Java in considering support for multiple inheritance of a compromise to consider it. Secondly, the definition of the abstract class, we can give the default behavior method. However, in the definition of interface, but n

17、ot with the default method, in order to circumvent this limitation, you must use commissioned, but it will be some additional complexity, and sometimes a lot of trouble. In the abstract class can not be defined in the default behavior of still another more serious issue, which is likely to cause tro

18、uble maintaining. If subsequently want to change the interface type (usually through abstract class or interface to express) to adapt to the new situation (for example, add a new method or methods have been used to add new parameters), will be very troublesome, may have to spend a lot of time (for m

19、any of the derived class, it is particularly the case). However, if the interface is achieved through abstract class, then may only need to amend the definition of the abstract class in the default behavior on it.Similarly, if not in the abstract class defined in the default behavior, it will lead t

20、o the same method appears in the abstract category in each derived class, in violation of the "one rule, one place" principle, resulting in duplication of code, and are not conducive to the future maintenance. Therefore, in the abstract class and the interface between the choices to be ver

21、y cautious.Design Concept perspective from the abstract class and interface:Mainly from the above definition of syntax and programming perspective on the interface of the abstract class and distinction, the difference between these levels are relatively low level, the Feibenzhi. This section will be

22、 another level: abstract class and interface design reflects the idea to an analysis of the difference between the two. The writer believes that, from this level of the two can understand the essence of the concept.As already mentioned, the abstract class in the Java language reflects a succession r

23、elations, in order to make reasonable inheritance, and the father of derived class must exist between the "is - a" relationship, that is, and the father of the concept of derivative nature should be the same. , The interface is not, does not require interface and the realization of the con

24、cept of interface definition is essentially the same, only the realization of the interface definition of lease it. In order to facilitate understanding discussed below will be through a simple example of a description.Consider this an example, we assumethat the problem areas, there is a Door on the

25、 abstract concept, with the implementation of the Door open and close the two movements, at this time we can abstract class or interface to the definition of an abstract concept that the type of definition of ways were as follows: Use abstract class defined Door:abstract class Door abstract void ope

26、n(); abstract void close(); Use interface defined Door:interface Doorvoid open(); void close();Other specific types Door extends the use of abstract class can be defined using the Door or implements the interface defined Door. Looks like interface and the use of abstract class does not have major di

27、fferences. If the request has to Door Alarm function. How are we going to design for the structure of the examples of this category (in this case, is to display abstract class and interface design concept reflected in the difference between the other issues unrelated to have done to simplify or igno

28、red)? Luo listed below will be possible solutions, and design the conceptual level from these options for analysis.Solution 1:Door in the simple definition of an additional alarm, as follows:abstract class Door abstract void open(); abstract void close(); abstract void alarm();Orinterface Doorvoid o

29、pen(); void close(); void alarm();So with alarm function AlarmDoor the definition as follows:class AlarmDoor extends Doorvoid ope n()void close() void alarm() Orclass AlarmDoor implements Door void ope n()void close() void alarm() This measure violates the object-oriented design of a core principle

30、ISP (Interface Segregation Principle), Door to Door in the definition of the concept of inherent behavior methods and another concept "alarm" act together in the mixed method. This is a problem caused by those who rely solely on the concept of the Door Module because of the "alarm&quo

31、t; the concept of this change (for example: Amending alarm method parameters) changed, still the contrary.Solution 2:Since the open, close and alarm are two different concepts, according to the principle of ISP should be representative of their respective definitions of these two concepts in the abs

32、tract category. Definition of ways: the use of these two concepts are abstract class defined; two concepts are defined to use interface and an abstract class concept of the use of defined, and the other concept of the use of defined interface.Clearly, the Java language does not support multiple inhe

33、ritance, the two concepts are defined using abstract class is not feasible. Behind both are feasible, but it is their choice in the field of the problem reflects the essenceof the concept of understanding, designed to reflect the correct and rational. We January 1 to analyze that.If the two concepts

34、 are used to define the interface mode, then reflects two problems: 1, we may not have a clear understanding of the field, AlarmDoor the concept is essentially in the end Door or alarm? 2, if we have no understanding of problem areas, such as: We have, through the analysis of problem areas found in

35、the concept of nature AlarmDoor and Door is the same, so when we realize there is no right to expose our design intent, because In both the definition of the concept (use interface are defined) do not reflect the meaning.If we understand the problem areas are: AlarmDoor the concept is essentially Do

36、or, at the same time it is the function of the police. How are we going to design and realize that we have to clear the meaning? Said earlier, the abstract class in the Java language that a succession, and inheritance, in essence, the "is - a" relationship. Therefore Door concept, we shoul

37、d use abstarct class approach to the definition. In addition, the alarm function is also AlarmDoor that it can complete the definition of the concept of the report, so the concept of alarm through the interface can be defined. As follows:abstract class Door abstract void open(); abstract void close(

38、); interface Alarm void alarm(); class Alarm Door extends Door implements Alarm void ope n()void close() void alarm() This basically means to realize that we have a clear understanding of the problem areas, and correctly expose our design intent. In fact, that is the abstract class "is - a"

39、; relationship, and that the interface is "like-a" relationship, we can choose as a basis, of course, becauseit is based on the understanding of the problem areas, such as: If we AlarmDoor think that the concept is essentially a warning, a Door at the same time, the function, then the abov

40、e definition of away to turn.Summary:1. abstract class in the Java language that is an inheritance, a class can only be used once inheritance. However, a class can implement multiple interface.2. Abstract class can have its own data members can be members of the non-abstarct, in the interface, only

41、to have static data can not be amended members (that is, must be static final, but in the interface generally not defined data members), all members of the methods are abstract.3. abstract class and interface design reflects the different concepts. In fact, that is the abstract class "is - a&qu

42、ot; relationship, and that the interface is "like-a" relationship.4. Realization of the abstract class and interface must implement all of these methods. An abstract class can be a non-abstract methods. Interface is not a method.5. Variables defined in the interface is the default public s

43、tatic final type, and must give its initial value, to achieve category can not be re-defined, not change its value.6. Abstract class is the default variable friendly type, and its value in the category of re-definition, can be re-assignment.7. Interface methods are public by default, the abstract ty

44、pe.Conclusion:Abstract class and the interface is Java language in the definition of the abstract category of two ways, and they have great similarity. But for their choice but often reflects problems in the area of understanding the nature of the concept, a reflection of the design intent is correc

45、t, reasonable, becausethey demonstrated the concept of the relationship between the different (although both can be achieved demand function). In fact, this is the kind of language usage, and hope that readers can detail of a friend.详细解析 Java 中抽象类和接口的区别在Java语言中,abstract class和in terface是支持抽象类定义的两种机

46、制。正是由于这两种机制的存在,才赋予了 Java 强大的 面向对象能力。 abstract class和in terface之间在对于抽象类定义的支持方面具有很大的相似 性,甚至可以相互替换, 因此很多开发者在进 行抽象类定义时对于 abstract class和in terface的选择显得比较随意。其实,两者之间还是有很大的区别 的,对于它们的选择甚至反映出对 于问题领域本质的理解、对于设计意图 的理解是否正确、合理。本文将对它们之间的区别进行一番剖析,试图给 开发者提供一个在二者之间进行选择的依据。理解抽象类:abstract class和in terface在Java语言中都是用来进行

47、抽象类(本文 中 的抽象类并非从abstract class翻译而来,它表示的是一个抽象体,而abstract class为Java语言中用于定义抽象类的一种方法,请读者注意区分)定义的,那么什么是抽象类,使用抽象类能为我们带来什么好处呢?在面向对象的概念中,我们知道所有的对象都是通过类来描绘的,但 是反过来却不是这样。并不是 所有的类都是用来描绘对象的,如果一个类 中没有包含足够的信息来描绘一个具体的对象,这样的类就是抽象类。抽 象类往往用来表征我们在对问题领域进行分析、 设计中得出的抽象概念, 是对一系列看上去不同,但是本质上相同的具体概念的抽象。比如:如果 我们进行一个图形编辑软件的开发

48、,就会发现问题领域存在着圆、 三角形 这样一些具体概念,它们是不同的,但是它们又都属于形状这样一个概念, 形状这个概念在问题领域是不存在的,它就是一个抽象概念。正是因为抽 象的概念 在问题领域没有对应的具体概念, 所以用以表征抽象概念的抽象 类是不能够实例化的。在面向对象领域,抽象类主要用来进行类型隐藏。 我们可以构造出一个固定的一组行为的抽象描 述,但是这组行为却能够有任意个可能的具体 实现方式。这个抽象描述就是抽象类,而这一组任意个可能的具体实现则 表现为所有可能的派生类。模块可以操作一个 抽象体。由于模块依赖于一 个固定的抽象体,因此它可以是不允许修改的;同时,通过从这个抽象体 派生,也

49、可扩展此模块的行为功能。熟悉OCP 的读者一定知 道,为了能够实现面向对象设计的一个最核心的原则OCP(Open-Closed Principle) ,抽象类是其中的关键所在。从语法定义层面看 abstract class 和 interface:在语法层面,Java语言对于abstract class和in terface给出了不同的定义 方式,下面以定义一个名为 Demo 的抽象类为例来说明这种不同。使用abstract class的方式定义Demo抽象类的方式如下:abstract class Demoabstract void method1();abstract void metho

50、d2();使用in terface的方式定义Demo抽象类的方式如下:interface Demovoid method1();void method2();在 abstract class 方式中, Demo 可以有自己的数据成员,也可以有非 abstract的成员方法,而在in terface方式的实现中,Demo只能够有静态的 不能被修改的数据成员(也就是必须是 static final 的,不过在 interface 中 一般不定义数据成员),所有的成员方法都是abstract的。从某种意义上说 in terface 是一种特殊形式的 abstract class从编程的角度来看,ab

51、stract class和in terface都可以用来实现"design by contract" 的思想。但是在具体的使用上面还是有一些区别的首先,abstract class在Java语言中表示的是一种继承关系,一个类只 能使用一次继承关系(因为Java不支持多继承-转注)。但是,一个类却可 以实现多个in terface。也许,这是Java语言的设计者在考虑Java对于多重 继承的支持方面的一种折中考虑吧。其次,在abstract class的定义中,我们可以赋予方法的默认行为。但是在 interface 的定义中,方法却不能拥有默 认行为,为了绕过这个限制,必须使

52、用委托,但是这会增加一些复杂性, 有时会造成很大的麻烦。 在 抽象类中不能定义默认行为还存在另一个比较 严重的问题,那就是可能会造成维护上的麻烦。因 为如果后来想修改类的 界面(一般通过 abstract class或者in terface来表示)以适应新的情况(比 如,添加新的方法或者给已用的方法中添 加新的参数)时,就会非常的麻 烦,可能要花费很多的时间(对于派生类很多的情况,尤为如此) 。但是如 果界面是通过abstract class来实现的,那么可能就只需要修改定义在abstract class中的默认行为就可以了。同样,如果不能在抽象类中定义默认行为,就会导致同样的方法实现 出现在

53、该抽象类的每一个派生类中,违反了 "one rule, one place" 原则, 造成代码重复,同样不利于以后的维护。因此,在abstract class和in terface间进行选择时要非常的小心。从设计理念层面看 abstract class 和 interface:上面主要从语法定义和编程的角度论述了abstract clas和interface的区别,这些层面的区别是比较低层次的、非本质的。本小节将从另一个层面: abstract class和in terface所反映出的设计理念,来分析一下二者的区别。作 者认为,从这个层面进行分析才能理解二者概念的本质所在

54、。前面已经提到过,abstract class在 Java语言中体现了一种继承关系,要 想使得 继承关系合理,父类和派生类之间必须存在"is-a"关系,即父类和派 生类在概念本质上应该是相同的。对于 interface 来说则不然,并不要求 interface 的实现者和 interface 定义在概念本质上是一致的, 仅仅是实现了 interface 定义的契约而已。 为了使论述便于理解, 下面将通过一个简单的实 例进行说明。考虑这样一个例子, 假设在我们的问题领域中有一个关于 Door 的抽象 概念,该Door具有执行两个动作open和close,此时我们可以通过abs

55、tract class或者in terface来定义一个表示该抽象概念的类型,定义方式分别如下 所示:使用 abstract class方式定义 Door:abstract class Doorabstract void open();abstract void close();使用 interface 方式定义 Door:interface Doorvoid open();void close();其他具体的Door类型可以extends使用abstract class方式定义的Door 或者impleme nts使用in terface方式定义的 Door。看起来好像使用 abstract

56、 class和in terface没有大的区别。如果现在要求 Door还要具有报警的功能。 我们该如何设计针对该例子的类结构呢(在本例中,主要是为了展示abstract class 和 interface 反映在设计理念上的区别,其他方面无关的问题 都做了简化或者忽略)?下面将罗列出可能的解 决方案,并从设计理念层 面对这些不同的方案进行分析。解决方案一:简单的在 Door 的定义中增加一个 alarm 方法,如下:abstract class Doorabstract void open();abstract void close(); abstract void alarm();或者int

57、erface Doorvoid open();void close();void alarm();那么具有报警功能的 AlarmDoor 的定义方式如下:class AlarmDoor extends Doorvoid ope n()void close()void alarm()或者class AlarmDoor implements Door void open() void close() void alarm() 这种 方法违反了面向对象设计中的一 个核 心原则 ISP (InterfaceSegregation Principle),在Door的定义中把 Door概念本身固有的行为方法 和另外一个概念 "报警器 "的行为方 法混在了一起。这样引起的一个问题是 那些仅仅依赖于 Door 这个概念的模块会因为 "报警器 "这个概念的改变(比 如:修改 alar

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论