为了正常的体验网站,请在浏览器设置里面开启Javascript功能!

JAVA外文资料翻译

2017-09-30 20页 doc 52KB 57阅读

用户头像

is_036899

暂无简介

举报
JAVA外文资料翻译JAVA外文资料翻译 外文文献原文及翻译 作 者:辛明 生物医学工程学院影像工程专业 指导老师:杨谊 生物医学工程学院信息技术系 Parsing Java Abstraction of the Difference Between Classes and Interfaces In Java language, abstract scale-up and with support class abstraction definition of two mechanisms. Because of these two ...
JAVA外文资料翻译
JAVA外文资料翻译 外文文献原文及翻译 作 者:辛明 生物医学工程学院影像工程专业 指导老师:杨谊 生物医学工程学院信息技术系 Parsing Java Abstraction of the Difference Between Classes and Interfaces In Java language, abstract scale-up and with support class abstraction definition of two mechanisms. Because of these two kinds of mechanism of existence, just gives Java powerful object-oriented skills. Abstract scale-up and with between class abstraction definition for support has great similarities, even interchangeable, so many developers into line nonabstract class definition for abstract scale-up and it is becoming more casual with choice. In fact, both between still has the very big difference, for their choice even reflected in problem domain essence of understanding, to design the intentions of the understand correctly and reasonable. This paper will for the difference analysis, trying to give a developer with a choice between them are based. Understand class abstraction Abstract class and interface in Java language is used for abstract classes (in this article nonabstract class not from abstract scale-up translation, it represents an abstract body, and abstract scale-up for Java language used to define class abstraction in one way, please readers distinguish) defined, then what are the abstract classes, use abstract classes for us any good? In object-oriented concept, we know all objects is through class to describe, but in turn not such. Not all classes are used to describe object, if a class does not contain enough information to portray a concrete object, this class is abstract classes. Abstract classes are often used to characterization of problem field in our analysis, design that the abstract concepts, is to the series will look different, but essentially the same exact conception of abstraction. For example: if we carry out a graphical editing software development, will find problem domain exists round, triangle so some specific concept, they are different, but they all belong to shape such a concept, shape this concept in problem domain is not exist, it is an abstract concept. Precisely because the abstract concepts in problem field no corresponding specific concept, so to characterization abstract concepts nonabstract class cannot be instantiated. In an object-oriented field, mainly used for class abstraction types hidden. We can construct a fixed a group of behavior of abstract description, but this group of behavior but can have any a possible concrete implementation. This abstract describe is abstract classes, and this an arbitrary a possible concrete realization is behaved for all possible derived class. Modules can be operating an abstract body. Due to the module dependent on a fixed abstraction body, so it can are not allowed to modify, Meanwhile, through the abstract derived from the body, also can expand the behavior of this module function. Familiar with OCP readers must know, object-oriented design to be able to achieve a core principles OCP (Open - Closed flying), class abstraction is one of the key. From the perspectives of grammar definition abstract class and interface In grammatical perspective, Java language for abstract scale-up and with gives different definitions below to define a way, called produce professional Demo abstract class as an example to illustrate the difference. Use the abstract scale-up produce professional Demo abstract class defined as follows: abstract class Demo, abstract void method1(); abstract void method2(); … , Use the way with produce professional Demo abstract class defined as follows: interface Demo{ void method1(); void method2(); … } In the abstract scale-up manner, produce professional Demo can have their own data members, also can have the members of the abstract method, and with the realization of the way, produce professional Demo can have only static cannot be modified data members (i.e. must is static, immigration, but in with generally doesn't define data members), all the members of the methods is abstract. In a sense, with a special kind of abstract class. From programming, from the perspective of abstract scale-up and with can be used to achieve "cancel" thoughts make themselves. But in the specific use top still have some difference. First, abstract class in Java language suggests is a kind of inheriting relationship, a class can be used only once inheritance relationship (because Java do not support more inheritance ZhuanZhu). - However, a class but can implement multiple with. Maybe it is Java language designers in considering Java for multiple inheritance support of a compromise to consider it. Secondly, in the definition of abstract scale-up, we may give methods of default behavior. But in with the definition of method cannot have the default behaviour, to bypass this limits, must use entrust, but it will add some complexity, sometimes can cause a lot of trouble. In class abstraction cannot define the default behaviour is there another serious problem that may cause on the maintenance of trouble. Because if later want to modify the interface (usually by such abstract scale-up or with to represent) to adapt to the new situation (e.g., adding new methods or to have already used the method to add new parameters), will be very troublesome, might spend a lot of time (for a derived class many situation, especially). But if the interface is through scale-up abstract to realize, then may just need to modify defined in the abstract scale-up default behavior is ok. Similarly, if not in abstract class defined in the default behavior, can lead to the same method to appear in the abstract class every a derived class, violated "a-one rule," principle, causing a-one place, the same code duplication against future maintenance. Therefore, in the abstract scale-up and with a choice between should be careful. From the design concept with abstract class and interface It mainly from grammar definition and programming perspective, this paper discusses the area with abstract class and don't, these levels difference is relatively low levels of, the essence. This section will from another level: abstract class and with reflected design concept, analyse the difference. The author thinks that from this level analysis to understand the essence of both concept. As already mentioned, abstract class in Java language reveals a kind of inheriting relationship, want to make reasonable, the inheritance relationship between parent class and derived class must exist "is - a" relations, namely the superclass and derived class in concept in essence should be the same. For with criterion otherwise, it does not require with of implementers and with defined in concept is essentially a consistent, only is realized with defined a contract is just. In order to facilitate understanding, discussed below will through a simple illustrated with examples. Consider such a example, suppose in our problem field has a about filled the abstract concepts, this filled with executive two movements open and close, then we can through scale-up or abstract with to define a said the abstract concept of type, define each pattern shown below: Use abstract class defined Door: abstract class Door{ abstract void open(); abstract void close(); } Use interface defined Door: interface Door{ void open(); void close(); } Other concrete filled type can use extends the abstract class defined or filled with defined using implements the filled. Looks like using abstract class and with no much difference. If now requires more filled with alarm function. How can we design according to the example of the class hierarchy? (in this case, it is mainly to show abstract class and with reflected in the design ideas, distinction, other aspect problem unrelated all did simplified or omitted)? Below will enumerate possible solutions, and from the design LiNianCeng face these different scheme for analysis. Solution a: Simple in filled the definition of adding a alarm method, as follows: abstract class Door{ abstract void open(); abstract void close(); abstract void alarm(); } or interface Door{ void open(); void close(); void alarm(); } So with alarm AlarmDoor defined as follows: class AlarmDoor extends Door{ void open(){…} void close(){…} void alarm(){…} } or class AlarmDoor implements Door, void open(){…} void close(){…} void alarm(){…} , This method violated the object-oriented design of a core principles ISP (with flying Segregation), in the definition of filled the filled concept itself inherent behavior methods and another concept "alarm" behavior methods mix together. Such a problem is that those who cause depends only upon the concept of modules will be held because "alarm" this concept change (for example: modify the parameters) and alarm method, and vice still change. Solution b: Since open and close and alarm belong to two different concepts, according to the ISP principle should consider them separately defined in representatives of these two concepts from the class abstraction. Definition means has: the two concepts are using abstract scale-up defined; Two concepts are used with defined; A concept using abstract scale-up defined, another concept using with defined. Obviously, due to Java language does not support multiple inheritance, so two concepts are using abstract class defined is not feasible. The latter two ways are feasible, but for their choice actually reflected in problem in the field of concept nature's understanding, whether for design intent reflect the correct and reasonable. We are a result analysis and description. If the two concepts are used with ways to define, then reflects two problems: 1, we may not understand clearly problem domain, AlarmDoor in concept essentially exactly is held or alarm? 2, if we in problem field understanding no problems, for example: we through for problem domain analysis found that AlarmDoor in concept in essence and filled is consistent, then we realize when he failed to correct reveal our design intention, because in these two concepts on the definitions (both use with defined) reflect reflected these meanings. If we in problem field understanding is: AlarmDoor in concept is essentially, at the same time it is filled with alarm function. How should we come to the design, realization to clear reflect what we mean by this? Front has said, in Java language abstract scale-up said in an inheritance relationship, and inheriting relationship is in nature "is a" relationship. So for held this concept, we should use abstarct scale-up way to define. In addition, AlarmDoor has alarm functions, that it will be able to accomplish alarm concept definition of behavior, so alarm concept can be through with defined. Shown below: abstract class Door{ abstract void open(); abstract void close(); } interface Alarm{ void alarm(); } class Alarm Door extends Door implements Alarm{ void open(){…} void close(){…} void alarm(){…} } This realization basically can clearly reflect our for problem domain, the correct understanding of our design intent reveals. Actually the abstract scale-up says is "is - a" relationship with said, was "like - a" relationship, everyone when the choice can be used as a basis, which, of course, is based on understanding the problem domain, for instance: if we think AlarmDoor in concept is essentially alarm, have again at the same time, then held the function of the above definition way will in turn. Summary 1. Abstract scale-up in Java language suggests is a kind of inheriting relationship, a class can be used only once inheritance relationship. However, a class but can implement multiple with. 2 in the abstract scale-up may have their own data members, also can have the members of the abstarct method, while in with, can have only static cannot be modified data members (i.e. must is static, immigration, but in with generally doesn't define data members), all the members of the methods is abstract. 3. With abstract scale-up and reflected the design concept of different. Actually the abstract scale-up says is "is - a" relationship with said, was "like - a" relationship. 4. Realize abstract classes and interface classes must realize, all of the method. Abstract classes may have not abstract methods. Interface cannot have realization method. 5. Interface definition of variable default is public, immigration, and static type to the initial value, so must realize class cannot be redefined, also can't change their values. 6. Class abstraction of variable default is cut type, the value that can be in subclasses redefined, ok also and new assignment. 7. Interface of the method are public default, abstract type. Conclusion Abstract class and with is Java language of two kinds of definition nonabstract class way, there are a great similarities. But for their choice but again often reflects on issues in the field of generalized read essence of understanding, to reflect the design intent is correct and reasonable, whether because they show the concept between different relation (although can realize the function demand). This is actually a kind of language of usage, like the reader friend can finely experience. 详细解析Java中抽象类和接口的区别 在Java语言中,abstract class和interface 是支持抽象类定义的两种机制。正是由于这两种机制的存在,才赋予了Java强大的 面向对象能力。abstract class和interface之间在 行抽对于抽象类定义的支持方面具有很大的相似性,甚至可以相互替换,因此很多开发者在进 象类定义时对于abstract class和interface的选择显得比较随意。其实,两者之间还是有很大的区别的,对于它们的选择甚至反映出对于问领域本质的理解、对于意图的理解是否正确、合理。本文将对它们之间的区别进行一番剖析,试图给开发者提供一个在二者之间进行选择的依据。 理解抽象类 abstract class和interface在Java语言中都是用来进行抽象类(本文中的抽象类并非从abstract class翻译而来,它表示的是一个抽象体,而abstract class为Java语言中用于定义抽象类的一种,请读者注意区分)定义的,那么什么是抽象类,使用抽象类能为我们带来什么好处呢, 在面向对象的概念中,我们知道所有的对象都是通过类来描绘的,但是反过来却不是这样。并不是所有的类都是用来描绘对象的,如果一个类中没有包含足够的信息来描绘一个具体的对象,这样的类就是抽象类。抽象类往往用来表征我们在对问题领域进行、设计中得出的抽象概念,是对一系列看上去不同,但是本质上相同的具体概念的抽象。比如:如果我们进行一个图形编辑软件的开发,就会发现问题领域存在着圆、三角形这样一些具体概念,它们是不同的,但是它们又都属于形状这样一个概念,形状这个概念在问题领域是不存在的,它就是一个抽象概念。正是因为抽象的概念在问题领域没有对应的具体概念,所以用以表征抽象概念的抽象类是不能够实例化的。 在面向对象领域,抽象类主要用来进行类型隐藏。我们可以构造出一个固定的一组行为的抽象描述,但是这组行为却能够有任意个可能的具体实现方式。这个抽象描述就是抽象类,而这一组任意个可能的具体实现则表现为所有可能的派生类。模块可以操作一个抽象体。由于模块依赖于一个固定的抽象体,因此它可以是不允许修改的;同时,通过从这个抽象体派生,也可扩展此模块的行为功能。熟悉OCP的读者一定知道,为了能够实现面向对象设计的一个最核心的原则OCP(Open-Closed Principle),抽象类是其中的关键所在。 从语法定义层面看abstract class 和 interface 在语法层面,Java语言对于abstract class和interface给出了不同的定义方式,下面以定义一个名为Demo的抽象类为例来说明这种不同。 使用abstract class的方式定义Demo抽象类的方式如下: abstract class Demo, abstract void method1(); abstract void method2(); … , 使用interface的方式定义Demo抽象类的方式如下: interface Demo{ void method1(); void method2(); … } 在abstract class方式中,Demo可以有自己的数据成员,也可以有非 abstract的成员方法,而在interface方式的实现中,Demo只能够有静态的不能被修改的数据成员(也就是必须是static final的,不过在interface中一般不定义数据成员),所有的成员方法都是abstract的。从某种意义上说,interface是一种特殊形式的abstract class。 从编程的角度来看,abstract class和interface都可以用来实现 "design by contract" 的思想。但是在具体的使用上面还是有一些区别的。 Java 语言中表示的是一种继承关系,一个类只能使用一次继承首先,abstract class 在 关系(因为Java不支持多继承 -- 转注)。但是,一个类却可以实现多个interface。也许,这是Java语言的设计者在考虑Java对于多重继承的支持方面的一种折中考虑吧。 其次,在abstract class的定义中,我们可以赋予方法的默认行为。但是在interface的定义中,方法却不能拥有默认行为,为了绕过这个限制,必须使用委托,但是这会增加一些复杂性,有时会造成很大的麻烦。 在抽象类中不能定义默认行为还存在另一个比较严重的问题,那就是可能会造成维护上的麻烦。因为如果后来想修改类的界面(一般通过 abstract class 或者interface来表示)以适应新的情况(比如,添加新的方法或者给已用的方法中添加新的参数)时,就会非常的麻烦,可能要花费很多的时间(对于派生类很多的情况,尤为如此)。但是如果界面是通过abstract class来实现的,那么可能就只需要修改定义在abstract class中的默认行为就可以了。 同样,如果不能在抽象类中定义默认行为,就会导致同样的方法实现出现在该抽象类的每一个派生类中,违反了 "one rule,one place" 原则,造成代码重复,同样不利于以后的维护。因此,在abstract class和interface间进行选择时要非常的小心。 从设计理念层面看 abstract class 和 interface 上面主要从语法定义和编程的角度论述了abstract class和interface的区 别,这些层面的区别是比较低层次的、非本质的。本小节将从另一个层面:abstract class和interface所反映出的设计理念,来分析一下二者的区别。作者认为,从这个层面进行分析才能理解二者概念的本质所在。 Java语言中体现了一种继承关系,要想使得继承关系前面已经提到过,abstract class在 合理,父类和派生类之间必须存在"is-a"关系,即父类和派生类在概念本质上应该是相同的。对于interface来说则不然,并不要求interface的实现者和interface定义在概念本质上是一致的,仅仅是实现了interface定义的契约而已。为了使论述便于理解,下面将通过一个简单的实例进行说明。 考虑这样一个例子,假设在我们的问题领域中有一个关于Door的抽象概念,该Door具有执行两个动作open和close,此时我们可以通过abstract class或者interface来定义一个表示该抽象概念的类型,定义方式分别如下所示: 使用abstract class方式定义Door: abstract class Door{ abstract void open(); abstract void close(); } 使用interface方式定义Door: interface Door{ void open(); void close(); } plements其他具体的Door类型可以extends使用abstract class方式定义的Door或者im使用interface方式定义的Door。看起来好像使用abstract class和interface没有大的区别。 如果现在要求Door还要具有报警的功能。我们该如何设计针对该例子的类结构呢(在本例 中,主要是为了展示 abstract class 和interface 反映在设计理念上的区别,其他方面无关 的问题都做了简化或者忽略),下面将罗列出可能的解决,并从设计理念层面对这些不同的 方案进行分析。 解决方案一: 简单的在Door的定义中增加一个alarm方法,如下: abstract class Door{ abstract void open(); abstract void close(); abstract void alarm(); } 或者 interface Door{ void open(); void close(); void alarm(); } 那么具有报警功能的AlarmDoor的定义方式如下: class AlarmDoor extends Door{ void open(){…} void close(){…} void alarm(){…} } 或者 class AlarmDoor implements Door, void open(){…} void close(){…} void alarm(){…} , 这种方法违反了面向对象设计中的一个核心原则ISP(Interface Segregation Principle), 在Door的定义中把Door概念本身固有的行为方法和另外一个概念"报警器"的行为方法混在了一 起。这样引起的一个问题是那些仅仅依赖于Door这个概念的模块会因为"报警器"这个概念的改变(比如:修改alarm方法的参数)而改变,反之依然。 解决方案二: 既然open、close和alarm属于两个不同的概念,根据ISP原则应该把它们分别定义在代表这两个概念的抽象类中。定义方式有:这两个概念都使用 abstract class 方式定义;两个概念都使用interface方式定义;一个概念使用 abstract class 方式定义,另一个概念使用interface方式定义。 显然,由于Java语言不支持多重继承,所以两个概念都使用abstract class方式定义是不可行的。后面两种方式都是可行的,但是对于它们的选择却反映出对于问题领域中的概念本质的理解、对于设计意图的反映是否正确、合理。我们一一来分析、说明。 如果两个概念都使用interface方式来定义,那么就反映出两个问题:1、我们可能没有理解清楚问题领域,AlarmDoor在概念本质上到底是Door还是报警器,2、如果我们对于问题领域的理解没有问题,比如:我们通过对于问题领域的分析发现AlarmDoor在概念本质上和Door是一致的,那么我们在实现时就没有能够正确的揭示我们的设计意图,因为在这两个概念的定义上(均使用 interface方式定义)反映不出上述含义。 如果我们对于问题领域的理解是:AlarmDoor在概念本质上是Door,同时它有具有报警的功能。我们该如何来设计、实现来明确的反映出我们的意思呢,前面已经说过,abstract class在Java语言中表示一种继承关系,而继承关系在本质上是"is-a"关系。所以对于Door这个概念,我们应该使用abstarct class方式来定义。另外,AlarmDoor又具有报警功能,说明它又能够完成报警概念中定义的行为,所以报警概念可以通过interface方式定义。如下所示: abstract class Door{ abstract void open(); abstract void close(); } interface Alarm{ void alarm(); } class Alarm Door extends Door implements Alarm{ void open(){…} void close(){…} void alarm(){…} } 这种实现方式基本上能够明确的反映出我们对于问题领域的理解,正确的揭示我们的设计意图。其实abstract class表示的是"is-a"关系,interface表示的是"like-a"关系,大家在选择时可以作为一个依据,当然这是建立在对问题领域的理解上的,比如:如果我们认为AlarmDoor在概念本质上是报警器,同时又具有Door的功能,那么上述的定义方式就要反过来了。 小结 1. abstract class 在Java语言中表示的是一种继承关系,一个类只能使用一次继承关系。但 是,一个类却可以实现多个interface。 2. 在abstract class 中可以有自己的数据成员,也可以有非abstarct的成员方法,而在 interface中,只能够有静态的不能被修改的数据成员(也就是必须是static final的, 不过在 interface中一般不定义数据成员),所有的成员方法都是abstract的。 3. abstract class和interface所反映出的设计理念不同。其实abstract class表示的是 "is-a"关系,interface表示的是"like-a"关系。 4. 实现抽象类和接口的类必须实现其中的所有方法。抽象类中可以有非抽象方法。接口中则不 能有实现方法。 5. 接口中定义的变量默认是public static final 型,且必须给其初值,所以实现类中不能 重新定义,也不能改变其值。 6. 抽象类中的变量默认是friendly型,其值可以在子类中重新定义,也可以重新赋值。 7. 接口中的方法默认都是 public,abstract 类型的。 结论 abstract class和interface是Java语言中的两种定义抽象类的方式,它们之间有很大的相似性。但是对于它们的选择却又往往反映出对于问题领域中的概 念本质的理解、对于设计意图的反映是否正确、合理,因为它们表现了概念间的不同的关系(虽然都能够实现需求的功能)。这其实也是语言的一种的惯用法,希望读者朋友能够细细体会。
/
本文档为【JAVA外文资料翻译】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。 本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。 网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。

历史搜索

    清空历史搜索