理工類畢業(yè)論文—英文文獻(xiàn)翻譯—Javadetailedanalysisofabstractclassesandinterfacesdistinction_第1頁
理工類畢業(yè)論文—英文文獻(xiàn)翻譯—Javadetailedanalysisofabstractclassesandinterfacesdistinction_第2頁
理工類畢業(yè)論文—英文文獻(xiàn)翻譯—Javadetailedanalysisofabstractclassesandinterfacesdistinction_第3頁
已閱讀5頁,還剩10頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

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.詳細(xì)解析 Java 中抽象類和接口的區(qū)別在Java語言中,abstract class和in terface是支持抽象類定義的兩種機(jī)

46、制。正是由于這兩種機(jī)制的存在,才賦予了 Java 強(qiáng)大的 面向?qū)ο竽芰Α?abstract class和in terface之間在對(duì)于抽象類定義的支持方面具有很大的相似 性,甚至可以相互替換, 因此很多開發(fā)者在進(jìn) 行抽象類定義時(shí)對(duì)于 abstract class和in terface的選擇顯得比較隨意。其實(shí),兩者之間還是有很大的區(qū)別 的,對(duì)于它們的選擇甚至反映出對(duì) 于問題領(lǐng)域本質(zhì)的理解、對(duì)于設(shè)計(jì)意圖 的理解是否正確、合理。本文將對(duì)它們之間的區(qū)別進(jìn)行一番剖析,試圖給 開發(fā)者提供一個(gè)在二者之間進(jìn)行選擇的依據(jù)。理解抽象類:abstract class和in terface在Java語言中都是用來進(jìn)行

47、抽象類(本文 中 的抽象類并非從abstract class翻譯而來,它表示的是一個(gè)抽象體,而abstract class為Java語言中用于定義抽象類的一種方法,請(qǐng)讀者注意區(qū)分)定義的,那么什么是抽象類,使用抽象類能為我們帶來什么好處呢?在面向?qū)ο蟮母拍钪?,我們知道所有的?duì)象都是通過類來描繪的,但 是反過來卻不是這樣。并不是 所有的類都是用來描繪對(duì)象的,如果一個(gè)類 中沒有包含足夠的信息來描繪一個(gè)具體的對(duì)象,這樣的類就是抽象類。抽 象類往往用來表征我們?cè)趯?duì)問題領(lǐng)域進(jìn)行分析、 設(shè)計(jì)中得出的抽象概念, 是對(duì)一系列看上去不同,但是本質(zhì)上相同的具體概念的抽象。比如:如果 我們進(jìn)行一個(gè)圖形編輯軟件的開發(fā)

48、,就會(huì)發(fā)現(xiàn)問題領(lǐng)域存在著圓、 三角形 這樣一些具體概念,它們是不同的,但是它們又都屬于形狀這樣一個(gè)概念, 形狀這個(gè)概念在問題領(lǐng)域是不存在的,它就是一個(gè)抽象概念。正是因?yàn)槌?象的概念 在問題領(lǐng)域沒有對(duì)應(yīng)的具體概念, 所以用以表征抽象概念的抽象 類是不能夠?qū)嵗?。在面向?qū)ο箢I(lǐng)域,抽象類主要用來進(jìn)行類型隱藏。 我們可以構(gòu)造出一個(gè)固定的一組行為的抽象描 述,但是這組行為卻能夠有任意個(gè)可能的具體 實(shí)現(xiàn)方式。這個(gè)抽象描述就是抽象類,而這一組任意個(gè)可能的具體實(shí)現(xiàn)則 表現(xiàn)為所有可能的派生類。模塊可以操作一個(gè) 抽象體。由于模塊依賴于一 個(gè)固定的抽象體,因此它可以是不允許修改的;同時(shí),通過從這個(gè)抽象體 派生,也

49、可擴(kuò)展此模塊的行為功能。熟悉OCP 的讀者一定知 道,為了能夠?qū)崿F(xiàn)面向?qū)ο笤O(shè)計(jì)的一個(gè)最核心的原則OCP(Open-Closed Principle) ,抽象類是其中的關(guān)鍵所在。從語法定義層面看 abstract class 和 interface:在語法層面,Java語言對(duì)于abstract class和in terface給出了不同的定義 方式,下面以定義一個(gè)名為 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 可以有自己的數(shù)據(jù)成員,也可以有非 abstract的成員方法,而在in terface方式的實(shí)現(xiàn)中,Demo只能夠有靜態(tài)的 不能被修改的數(shù)據(jù)成員(也就是必須是 static final 的,不過在 interface 中 一般不定義數(shù)據(jù)成員),所有的成員方法都是abstract的。從某種意義上說 in terface 是一種特殊形式的 abstract class從編程的角度來看,ab

51、stract class和in terface都可以用來實(shí)現(xiàn)"design by contract" 的思想。但是在具體的使用上面還是有一些區(qū)別的首先,abstract class在Java語言中表示的是一種繼承關(guān)系,一個(gè)類只 能使用一次繼承關(guān)系(因?yàn)镴ava不支持多繼承-轉(zhuǎn)注)。但是,一個(gè)類卻可 以實(shí)現(xiàn)多個(gè)in terface。也許,這是Java語言的設(shè)計(jì)者在考慮Java對(duì)于多重 繼承的支持方面的一種折中考慮吧。其次,在abstract class的定義中,我們可以賦予方法的默認(rèn)行為。但是在 interface 的定義中,方法卻不能擁有默 認(rèn)行為,為了繞過這個(gè)限制,必須使

52、用委托,但是這會(huì)增加一些復(fù)雜性, 有時(shí)會(huì)造成很大的麻煩。 在 抽象類中不能定義默認(rèn)行為還存在另一個(gè)比較 嚴(yán)重的問題,那就是可能會(huì)造成維護(hù)上的麻煩。因 為如果后來想修改類的 界面(一般通過 abstract class或者in terface來表示)以適應(yīng)新的情況(比 如,添加新的方法或者給已用的方法中添 加新的參數(shù))時(shí),就會(huì)非常的麻 煩,可能要花費(fèi)很多的時(shí)間(對(duì)于派生類很多的情況,尤為如此) 。但是如 果界面是通過abstract class來實(shí)現(xiàn)的,那么可能就只需要修改定義在abstract class中的默認(rèn)行為就可以了。同樣,如果不能在抽象類中定義默認(rèn)行為,就會(huì)導(dǎo)致同樣的方法實(shí)現(xiàn) 出現(xiàn)在

53、該抽象類的每一個(gè)派生類中,違反了 "one rule, one place" 原則, 造成代碼重復(fù),同樣不利于以后的維護(hù)。因此,在abstract class和in terface間進(jìn)行選擇時(shí)要非常的小心。從設(shè)計(jì)理念層面看 abstract class 和 interface:上面主要從語法定義和編程的角度論述了abstract clas和interface的區(qū)別,這些層面的區(qū)別是比較低層次的、非本質(zhì)的。本小節(jié)將從另一個(gè)層面: abstract class和in terface所反映出的設(shè)計(jì)理念,來分析一下二者的區(qū)別。作 者認(rèn)為,從這個(gè)層面進(jìn)行分析才能理解二者概念的本質(zhì)所在

54、。前面已經(jīng)提到過,abstract class在 Java語言中體現(xiàn)了一種繼承關(guān)系,要 想使得 繼承關(guān)系合理,父類和派生類之間必須存在"is-a"關(guān)系,即父類和派 生類在概念本質(zhì)上應(yīng)該是相同的。對(duì)于 interface 來說則不然,并不要求 interface 的實(shí)現(xiàn)者和 interface 定義在概念本質(zhì)上是一致的, 僅僅是實(shí)現(xiàn)了 interface 定義的契約而已。 為了使論述便于理解, 下面將通過一個(gè)簡(jiǎn)單的實(shí) 例進(jìn)行說明??紤]這樣一個(gè)例子, 假設(shè)在我們的問題領(lǐng)域中有一個(gè)關(guān)于 Door 的抽象 概念,該Door具有執(zhí)行兩個(gè)動(dòng)作open和close,此時(shí)我們可以通過abs

55、tract class或者in terface來定義一個(gè)表示該抽象概念的類型,定義方式分別如下 所示:使用 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沒有大的區(qū)別。如果現(xiàn)在要求 Door還要具有報(bào)警的功能。 我們?cè)撊绾卧O(shè)計(jì)針對(duì)該例子的類結(jié)構(gòu)呢(在本例中,主要是為了展示abstract class 和 interface 反映在設(shè)計(jì)理念上的區(qū)別,其他方面無關(guān)的問題 都做了簡(jiǎn)化或者忽略)?下面將羅列出可能的解 決方案,并從設(shè)計(jì)理念層 面對(duì)這些不同的方案進(jìn)行分析。解決方案一:簡(jiǎn)單的在 Door 的定義中增加一個(gè) alarm 方法,如下:abstract class Doorabstract void open();abstract void close(); abstract void alarm();或者int

57、erface Doorvoid open();void close();void alarm();那么具有報(bào)警功能的 AlarmDoor 的定義方式如下:class AlarmDoor extends Doorvoid ope n()void close()void alarm()或者class AlarmDoor implements Door void open() void close() void alarm() 這種 方法違反了面向?qū)ο笤O(shè)計(jì)中的一 個(gè)核 心原則 ISP (InterfaceSegregation Principle),在Door的定義中把 Door概念本身固有的行為方法 和另外一個(gè)概念 "報(bào)警器 "的行為方 法混在了一起。這樣引起的一個(gè)問題是 那些僅僅依賴于 Door 這個(gè)概念的模塊會(huì)因?yàn)?"報(bào)警器 "這個(gè)概念的改變(比 如:修改 alar

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論