当前位置:文档之家› (完整版)毕设外文翻译-详细解析Java中抽象类和接口的区别

(完整版)毕设外文翻译-详细解析Java中抽象类和接口的区别

Parsing Java Abstraction of the Difference Between Classes andInterfacesIn 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 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 difference, 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 abstractionAbstract 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, 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 non-abstract class cannot be instantiated.In an object-oriented field, mainly used for class abstraction types hidden. We canconstruct 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 principle OCP (Open - Closed flying), class abstraction is one of the key.From the perspectives of grammar definition abstract class and interfaceIn 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.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 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 behavior, 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 behavior is there another serious problem that may cause on the maintenance of trouble. Because if later want to modify theinterface (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 interfaceIt 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, 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 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 super class 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 understandings.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 .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 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 withreflected 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 schemes for analysis.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.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 forheld this concept, we should use abstract 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.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.Summary1. 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 abstract 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.ConclusionAbstract 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 inthe 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 是支持抽象类定义的两种机制。

相关主题