《2022年毕设外文翻译-详细解析Java中抽象类和接口的区别.docx》由会员分享,可在线阅读,更多相关《2022年毕设外文翻译-详细解析Java中抽象类和接口的区别.docx(23页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、精选学习资料 - - - - - - - - - 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 skill
2、s. Abstract scale-up and with between classes abstraction definition for support has great similarities, even interchangeable, so many developers into line non-abstract class definition for abstract scale-up and it is becoming more casual with choice. In fact, both between still has the very big dif
3、ference, for their choice even reflected in problem domain essence of understanding, to design the intentions of the understanding 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 Abst
4、ract class and interface in Java language is used for abstract classes in this article non-abstract 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, t
5、hen 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
6、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 softwar
7、e 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 correspo
8、nding specific concept, so to characterization abstract concepts non-abstract class cannot be instantiated. In an object-oriented field, mainly used for class abstraction types hidden. We can 名师归纳总结 - - - - - - -第 1 页,共 12 页精选学习资料 - - - - - - - - - construct a fixed a group of behavior of abstract d
9、escription, 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 depend
10、ent 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 principle OCP Open - Closed flyi
11、ng, 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
12、to illustrate the difference. 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, all the mem
13、bers 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 langu
14、age 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 suppor
15、t 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 behavior, to bypass this limits, must use entrust, but it will add some complexity, sometimes can cause a lot of t
16、rouble. In class abstraction cannot define the default behavior is there another serious problem that may cause on the maintenance of trouble. Because if later want to modify the 名师归纳总结 - - - - - - -第 2 页,共 12 页精选学习资料 - - - - - - - - - interface usually by such abstract scale-up or with to represent
17、 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 mo
18、dify 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
19、 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 dont, these levels diff
20、erence is relatively low levels of, the essence. This section will from another level: abstract class and with reflected design concept, analyst the difference. The author thinks that from this level analysis to understand the essence of both concepts. As already mentioned, abstract class in Java la
21、nguage 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 super class and derived class in concept in essence should be the same. For with criterion otherwise, it does not req
22、uire 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 understandings. Consider such a example, suppose in our problem field has a about filled the abstract concepts, this filled with executive two m
23、ovements open and close, then we can through scale-up or abstract with to define a said the abstract concept of type, define each pattern . Other concrete filled type can use extends the abstract class defined or filled with defined using implements the filled. Look like using abstract class and wit
24、h 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 名师归纳总结 - - - - - - -第 3 页,共 12 页精选学习资料 - - - - - - - - - reflected in the design ideas, distinction, ot
25、her aspect problem unrelated all did simplified or omitted. Below will enumerate possible solutions, and from the design LiNianCeng face these different schemes for analysis. This method violated the object-oriented design of a core principles ISP with flying Segregation, in the definition of filled
26、 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 s
27、till change. 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 co
28、ncepts 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
29、 actually reflected in problem in the field of concept natures 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
30、 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 desig
31、n 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 clea
32、r 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 名师归纳总结 - - - - - - -第 4 页,共 12 页精选学习资料 - - - - - - - - - held this concept, we should use abstract scale-up way t
33、o 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. This realization basically can clearly reflect our for problem domain, the correct understanding of our design intent reveals.
34、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 sam
35、e 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 m
36、ay have their own data members, also can have the members of the abstract method, while in with, can have only static cannot be modified data members i.e. must is static, immigration, but in with generally doesnt define data members, all the members of the methods is abstract. 3. With abstract scale
37、-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
38、 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 cant change their values. 6. Class abstraction of variable default is cut type, the value that can be in subclasses redefined
39、, 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 non-abstract class way, there are a great similarities. But for their choice but again often reflects on issues in 名师归纳总结 - - - -
40、 - - -第 5 页,共 12 页精选学习资料 - - - - - - - - - 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
41、 usage, like the reader friend can finely experience. 名师归纳总结 - - - - - - -第 6 页,共 12 页精选学习资料 - - - - - - - - - 具体解析 Java中抽象类和接口的区分在 Java语言中,abstract class和 interface 是支持抽象类定义的两种机制; 正是由于这两种机制的存在,才给予了Java 强大的面对对象才能; abstract class和 interface之间在对于抽象类定义的支持方面具有很大的相像性,甚至可以相互替换, 因此许多开发者在进 行抽象类定义时对于 abstrac
42、t class和 interface 的挑选显得比较随便;其实,两者之间仍是有很大的区分的, 对于它们的挑选甚至反映出对于问题领域本质的懂得、对于设计意图的懂得是否正确、 合理;本文将对它们之间的区分进行一番剖析,试图给开发者供应一个在二者之间进行挑选的依据;懂得抽象类abstract class和 interface 在 Java语言中都是用来进行抽象类 (本文中的抽象类并非从 abstract class翻译而来, 它表示的是一个抽象体, 而 abstract class为 Java语言中用于定义抽象类的一种方法,请读者留意区分)定义的,那么什么是抽象类,使用抽象类能为我们带来什么好处呢?
43、在面对对象的概念中, 我们知道全部的对象都是通过类来描画的,但是反过来却不是这样; 并不是全部的类都是用来描画对象的,假如一个类中没有包含足够的信息来描画一个具体的对象, 这样的类就是抽象类; 抽象类往往用来表征我们在对问题领 域进行分析、 设计中得出的抽象概念, 是对一系列看上去不同, 但是本质上相同的具 体概念的抽象; 比如:假如我们进行一个图形编辑软件的开发,就会发觉问题领域存 在着圆、 三角形这样一些具体概念, 它们是不同的, 但是它们又都属于外形这样一个概念,外形这个概念在问题领域是不存在的,它就是一个抽象概念; 正是由于抽象的概念在问题领域没有对应的具体概念,所以用以表征抽象概念的
44、抽象类是不能够实例化的;在面对对象领域, 抽象类主要用来进行类型隐匿; 我们可以构造出一个固定的一组行为的抽象描述, 但是这组行为却能够有任意个可能的具体实现方式;这个抽象描述就是抽象类, 而这一组任意个可能的具体实现就表现为全部可能的派生类;模块可以操作一个抽象体; 由于模块依靠于一个固定的抽象体, 因此它可以是不答应修改的;名师归纳总结 同时,通过从这个抽象体派生, 也可扩展此模块的行为功能;熟识 OCP的读者肯定知第 7 页,共 12 页- - - - - - -精选学习资料 - - - - - - - - - 道,为了能够实现面对对象设计的一个最核心的原就 象类是其中的关键所在;OCP
45、Open-Closed Principle,抽从语法定义层面看 abstract class和 interface在语法层面, Java语言对于 abstract class和 interface 给出了不同的定义方式,下面以定义一个名为 Demo 的抽象类为例来说明这种不同;在 abstract class 方式中, Demo 可以有自己的数据成员,也可以有非 abstract的成员方法,而在 interface方式的实现中, Demo 只能够有静态的不能被修改的数据成员,全部的成员方法都是 的 abstract class;abstract的;从某种意义上说, interface 是一种特
46、别形式从编程的角度来看, abstract class和 interface都可以用来实现 design by contract 的思想;但是在具体的使用上面仍是有一些区分的;第一, abstract class 在 Java 语言中表示的是一种继承关系,一个类只能使用一次继承关系 由于Java 不支持多继承 - 转注 ;但是,一个类却可以实现多个interface;或许,这是 Java语言的设计者在考虑 折中考虑吧;Java对于多重继承的支持方面的一种其次,在 abstract class的定义中,我们可以给予方法的默认行为; 但是在 interface 的定义中,方法却不能拥有默认行为,为
47、了绕过这个限制,必需使用托付,但是这会 增加一些复杂性,有时会造成很大的麻烦;在抽象类中不能定义默认行为仍存在另一个比较严峻的问题,那就是可能会造成保护上的麻烦;由于假如后来想修改类的界面(一般通过abstract class或 interface来表示)以适应新的情形(比如,添加新的方法或者给已用的方法中添加新的参数)时,就会特别的麻烦, 可能要花费许多的时间 (对于派生类许多的情形, 尤为如此) ;但是假如界面是通过abstract class来实现的,那么可能就只需要修改定义在abstract class中的默认行为就可以了;同样,假如不能在抽象类中定义默认行为,就会导致同样的方法实现显现在该抽象类的每一个派生类中,违反了 one rule,one place 原就,造成代码重复,同样不利于以后的保护;因此,在abstract class和 interface 间进行挑选时要特别的当心;名师归纳总结 从设计理念层面看abstract class和 interface第 8 页,共 12 页- - - - - - -精选学习资料 - - - - - - - - - 上面主要从语法定义和编程的角度论述了abstract class和 interface 的区 别,这些层面的区分是比较低层次的、非本质的;本小节将从另一个层面:abstract class和interf
限制150内