2023年全國碩士研究生考試考研英語一試題真題(含答案詳解+作文范文)_第1頁
已閱讀1頁,還剩12頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡介

1、<p>  Parsing Java Abstraction of the Difference Between Classes and Interfaces</p><p>  In Java language, abstract scale-up and with support class abstraction definition of two mechanisms. Because of t

2、hese 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 ma

3、ny 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 di</p><p>  Understand class abstraction&l

4、t;/p><p>  Abstract 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 sca

5、le-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?</p><p>  In object-oriented

6、 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 ab

7、stract 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

8、abstraction. For example: if we carry out </p><p>  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

9、 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

10、 can be operating an abstract body. Due to the module dependent on a fixed abstraction body, so it can are not allowed to mod</p><p>  From the perspectives of grammar definition abstract class and interface

11、</p><p>  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 illustra

12、te the difference.</p><p>  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,

13、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.</p><p>  From programming, from

14、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.</p><p>  First, abstract class in Jav

15、a 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

16、. Maybe it is Java language designers in considering Java for multiple inheritance support of a compromise to consider it.</p><p>  Secondly, in the definition of abstract scale-up, we may give methods of de

17、fault 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.</p><p> 

18、 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 interface (usually by such abstract scale-up or

19、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 situatio

20、n, especially). But if the interface is through scale-up abst</p><p>  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

21、 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.</

22、p><p>  From the design concept with abstract class and interface</p><p>  It mainly from grammar definition and programming perspective, this paper discusses the area with abstract class and don&#

23、39;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 lev

24、el analysis to understand the essence of both concepts.</p><p>  As already mentioned, abstract class in Java language reveals a kind of inheriting relationship, want to make reasonable, the inheritance rela

25、tionship 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 requir

26、e with of implementers and with defined in concept is essentially a consistent, only is realized with defined a contract is just. In order to facili</p><p>  Consider such a example, suppose in our problem f

27、ield 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 .&l

28、t;/p><p>  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.</p><p&

29、gt;  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, distinct

30、ion, 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.</p><p>  This method vio

31、lated 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

32、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 sti

33、ll change.</p><p>  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 c

34、lass 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.</p>

35、<p>  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 reflecte

36、d 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.</p><p>  If the two concepts are used

37、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 exampl

38、e: 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 d

39、efinitions (both use with defin</p><p>  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, rea

40、lization 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

41、this concept, we should use abstract scale-up way to define. In addition, AlarmDoor has alarm functions, that it will be abl</p><p>  This realization basically can clearly reflect our for problem domain, th

42、e 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 bas

43、is, 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 wil

44、l in tu</p><p><b>  Summary</b></p><p>  1. Abstract scale-up in Java language suggests is a kind of inheriting relationship, a class can be used only once inheritance relationship.

45、However, a class but can implement multiple with.</p><p>  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 ca

46、nnot 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.</p><p>  3. With abstract scale-up and ref

47、lected the design concept of different. Actually the abstract scale-up says is "is - a" relationship with said, was "like - a" relationship.</p><p>  4. Realize abstract classes and inter

48、face classes must realize, all of the method. Abstract classes may have not abstract methods. Interface cannot have realization method.</p><p>  5. Interface definition of variable default is public, immigra

49、tion, and static type to the initial value, so must realize class cannot be redefined, also can't change their values.</p><p>  6. Class abstraction of variable default is cut type, the value that can be

50、 in subclasses redefined, ok also and new assignment.</p><p>  7. Interface of the method are public default, abstract type.</p><p>  Conclusion</p><p>  Abstract class and with is

51、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 the field of generalized read essence of understanding, to refle

52、ct 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 f

53、riend can finely experience.</p><p>  詳細(xì)解析Java中抽象類和接口的區(qū)別</p><p>  在Java語言中,abstract class和interface 是支持抽象類定義的兩種機(jī)制。正是由于這兩種機(jī)制的存在,才賦予了Java強(qiáng)大的面向?qū)ο竽芰?。abstract class和interface之間在對于抽象類定義的支持方面具有很大的相似性,甚

54、至可以相互替換,因此很多開發(fā)者在進(jìn) 行抽象類定義時(shí)對于abstract class和interface的選擇顯得比較隨意。其實(shí),兩者之間還是有很大的區(qū)別的,對于它們的選擇甚至反映出對于問題領(lǐng)域本質(zhì)的理解、對于設(shè)計(jì)意圖的理解是否正確、合理。本文將對它們之間的區(qū)別進(jìn)行一番剖析,試圖給開發(fā)者提供一個在二者之間進(jìn)行選擇的依據(jù)。</p><p><b>  理解抽象類</b></p>&

55、lt;p>  abstract class和interface在Java語言中都是用來進(jìn)行抽象類(本文中的抽象類并非從abstract class翻譯而來,它表示的是一個抽象體,而abstract class為Java語言中用于定義抽象類的一種方法,請讀者注意區(qū)分)定義的,那么什么是抽象類,使用抽象類能為我們帶來什么好處呢?</p><p>  在面向?qū)ο蟮母拍钪?,我們知道所有的對象都是通過類來描繪的,但是

56、反過來卻不是這樣。并不是所有的類都是用來描繪對象的,如果一個類中沒有包含足夠的信息來描繪一個具體的對象,這樣的類就是抽象類。抽象類往往用來表征我們在對問題領(lǐng)域進(jìn)行分析、設(shè)計(jì)中得出的抽象概念,是對一系列看上去不同,但是本質(zhì)上相同的具體概念的抽象。比如:如果我們進(jìn)行一個圖形編輯軟件的開發(fā),就會發(fā)現(xiàn)問題領(lǐng)域存在著圓、三角形這樣一些具體概念,它們是不同的,但是它們又都屬于形狀這樣一個概念,形狀這個概念在問題領(lǐng)域是不存在的,它就是一個抽象概念。正

57、是因?yàn)槌橄蟮母拍钤趩栴}領(lǐng)域沒有對應(yīng)的具體概念,所以用以表征抽象概念的抽象類是不能夠?qū)嵗摹?lt;/p><p>  在面向?qū)ο箢I(lǐng)域,抽象類主要用來進(jìn)行類型隱藏。我們可以構(gòu)造出一個固定的一組行為的抽象描述,但是這組行為卻能夠有任意個可能的具體實(shí)現(xiàn)方式。這個抽象描述就是抽象類,而這一組任意個可能的具體實(shí)現(xiàn)則表現(xiàn)為所有可能的派生類。模塊可以操作一個抽象體。由于模塊依賴于一個固定的抽象體,因此它可以是不允許修改的;同時(shí),通

58、過從這個抽象體派生,也可擴(kuò)展此模塊的行為功能。熟悉OCP的讀者一定知道,為了能夠?qū)崿F(xiàn)面向?qū)ο笤O(shè)計(jì)的一個最核心的原則OCP(Open-Closed Principle),抽象類是其中的關(guān)鍵所在。</p><p>  從語法定義層面看abstract class 和 interface</p><p>  在語法層面,Java語言對于abstract class和interface給出了不同的

59、定義方式,下面以定義一個名為Demo的抽象類為例來說明這種不同。</p><p>  在abstract class方式中,Demo可以有自己的數(shù)據(jù)成員,也可以有非 abstract的成員方法,而在interface方式的實(shí)現(xiàn)中,Demo只能夠有靜態(tài)的不能被修改的數(shù)據(jù)成員,所有的成員方法都是abstract的。從某種意義上說,interface是一種特殊形式的abstract class。</p>

60、<p>  從編程的角度來看,abstract class和interface都可以用來實(shí)現(xiàn) "design by contract" 的思想。但是在具體的使用上面還是有一些區(qū)別的。</p><p>  首先,abstract class 在 Java 語言中表示的是一種繼承關(guān)系,一個類只能使用一次繼承關(guān)系(因?yàn)镴ava不支持多繼承 -- 轉(zhuǎn)注)。但是,一個類卻可以實(shí)現(xiàn)多個inter

61、face。也許,這是Java語言的設(shè)計(jì)者在考慮Java對于多重繼承的支持方面的一種折中考慮吧。</p><p>  其次,在abstract class的定義中,我們可以賦予方法的默認(rèn)行為。但是在interface的定義中,方法卻不能擁有默認(rèn)行為,為了繞過這個限制,必須使用委托,但是這會增加一些復(fù)雜性,有時(shí)會造成很大的麻煩。</p><p>  在抽象類中不能定義默認(rèn)行為還存在另一個比較嚴(yán)

62、重的問題,那就是可能會造成維護(hù)上的麻煩。因?yàn)槿绻髞硐胄薷念惖慕缑妫ㄒ话阃ㄟ^abstract class或interface來表示)以適應(yīng)新的情況(比如,添加新的方法或者給已用的方法中添加新的參數(shù))時(shí),就會非常的麻煩,可能要花費(fèi)很多的時(shí)間(對于派生類很多的情況,尤為如此)。但是如果界面是通過abstract class來實(shí)現(xiàn)的,那么可能就只需要修改定義在abstract class中的默認(rèn)行為就可以了。</p><p

63、>  同樣,如果不能在抽象類中定義默認(rèn)行為,就會導(dǎo)致同樣的方法實(shí)現(xiàn)出現(xiàn)在該抽象類的每一個派生類中,違反了 "one rule,one place" 原則,造成代碼重復(fù),同樣不利于以后的維護(hù)。因此,在abstract class和interface間進(jìn)行選擇時(shí)要非常的小心。</p><p>  從設(shè)計(jì)理念層面看abstract class和interface</p><

64、p>  上面主要從語法定義和編程的角度論述了abstract class和interface的區(qū) 別,這些層面的區(qū)別是比較低層次的、非本質(zhì)的。本小節(jié)將從另一個層面:abstract class和interface所反映出的設(shè)計(jì)理念,來分析一下二者的區(qū)別。作者認(rèn)為,從這個層面進(jìn)行分析才能理解二者概念的本質(zhì)所在。</p><p>  前面已經(jīng)提到過,abstract class在Java語言中體現(xiàn)了一種繼承關(guān)系

65、,要想使得繼承關(guān)系合理,父類和派生類之間必須存在"is-a"關(guān)系,即父類和派生類在概念本質(zhì)上應(yīng)該是相同的。對于interface來說則不然,并不要求interface的實(shí)現(xiàn)者和interface定義在概念本質(zhì)上是一致的,僅僅是實(shí)現(xiàn)了interface定義的契約而已。為了使論述便于理解,下面將通過一個簡單的實(shí)例進(jìn)行說明。</p><p>  考慮這樣一個例子,假設(shè)在我們的問題領(lǐng)域中有一個關(guān)于Do

66、or的抽象概念,該Door具有執(zhí)行兩個動作open和close,此時(shí)我們可以通過abstract class或者interface來定義一個表示該抽象概念的類型。</p><p>  其他具體的Door類型可以extends使用abstract class方式定義的Door或者implements使用interface方式定義的Door??雌饋砗孟袷褂胊bstract class和interface沒有大的區(qū)別。

67、</p><p>  如果現(xiàn)在要求Door還要具有報(bào)警的功能。我們該如何設(shè)計(jì)針對該例子的類結(jié)構(gòu)呢(在本例中,主要是為了展示abstract class和interface反映在設(shè)計(jì)理念上的區(qū)別,其他方面無關(guān)的問題都做了簡化或者忽略)?下面將羅列出可能的解決方案,并從設(shè)計(jì)理念層面對這些不同的方案進(jìn)行分析。</p><p>  這種方法違反了面向?qū)ο笤O(shè)計(jì)中的一個核心原則ISP(Interfac

68、e Segregation Principle),在Door的定義中把Door概念本身固有的行為方法和另外一個概念"報(bào)警器"的行為方法混在了一起。這樣引起的一個問題是那些僅僅依賴于Door這個概念的模塊會因?yàn)?quot;報(bào)警器"這個概念的改變(比如:修改alarm方法的參數(shù))而改變,反之依然。</p><p>  既然open、close和alarm屬于兩個不同的概念,根據(jù)ISP原則

69、應(yīng)該把它們分別定義在代表這兩個概念的抽象類中。定義方式有:這兩個概念都使用abstract class方式定義;兩個概念都使用interface方式定義;一個概念使用abstract class方式定義,另一個概念使用interface方式定義。</p><p>  顯然,由于Java語言不支持多重繼承,所以兩個概念都使用abstract class方式定義是不可行的。后面兩種方式都是可行的,但是對于它們的選擇卻

70、反映出對于問題領(lǐng)域中的概念本質(zhì)的理解、對于設(shè)計(jì)意圖的反映是否正確、合理。我們一一來分析、說明。</p><p>  如果兩個概念都使用interface方式來定義,那么就反映出兩個問題:1、我們可能沒有理解清楚問題領(lǐng)域,AlarmDoor在概念本質(zhì)上到底是Door還是報(bào)警器?2、如果我們對于問題領(lǐng)域的理解沒有問題,比如:我們通過對于問題領(lǐng)域的分析發(fā)現(xiàn)AlarmDoor在概念本質(zhì)上和Door是一致的,那么我們在實(shí)現(xiàn)

71、時(shí)就沒有能夠正確的揭示我們的設(shè)計(jì)意圖,因?yàn)樵谶@兩個概念的定義上(均使用interface方式定義)反映不出上述含義。</p><p>  如果我們對于問題領(lǐng)域的理解是:AlarmDoor在概念本質(zhì)上是Door,同時(shí)它有具有報(bào)警的功能。我們該如何來設(shè)計(jì)、實(shí)現(xiàn)來明確的反映出我們的意思呢?前面已經(jīng)說過,abstract class在Java語言中表示一種繼承關(guān)系,而繼承關(guān)系在本質(zhì)上是"is-a"關(guān)系

72、。所以對于Door這個概念,我們應(yīng)該使用abstract class方式來定義。另外,AlarmDoor又具有報(bào)警功能,說明它又能夠完成報(bào)警概念中定義的行為,所以報(bào)警概念可以通過interface方式定義。</p><p>  這種實(shí)現(xiàn)方式基本上能夠明確的反映出我們對于問題領(lǐng)域的理解,正確的揭示我們的設(shè)計(jì)意圖。其實(shí)abstract class表示的是"is-a"關(guān)系,interface表示的是

73、"like-a"關(guān)系,大家在選擇時(shí)可以作為一個依據(jù),當(dāng)然這是建立在對問題領(lǐng)域的理解上的,比如:如果我們認(rèn)為AlarmDoor在概念本質(zhì)上是報(bào)警器,同時(shí)又具有Door的功能,那么上述的定義方式就要反過來了。</p><p><b>  小結(jié)</b></p><p>  abstract class在Java語言中表示的是一種繼承關(guān)系,一個類只能使用一

74、次繼承關(guān)系。但是,一個類卻可以實(shí)現(xiàn)多個interface。</p><p>  在abstract class中可以有自己的數(shù)據(jù)成員,也可以有非abstract的成員方法,而在interface中,只能夠有靜態(tài)的不能被修改的數(shù)據(jù)成員(也就是必須是static final的,不過在 interface中一般不定義數(shù)據(jù)成員),所有的成員方法都是abstract的。</p><p>  abst

75、ract class和interface所反映出的設(shè)計(jì)理念不同。其實(shí)abstract class表示的是"is-a"關(guān)系,interface表示的是"like-a"關(guān)系。</p><p>  實(shí)現(xiàn)抽象類和接口的類必須實(shí)現(xiàn)其中的所有方法。抽象類中可以有非抽象方法。接口中則不能有實(shí)現(xiàn)方法。</p><p>  接口中定義的變量默認(rèn)是public stat

76、ic final型,且必須給其初值,所以實(shí)現(xiàn)類中不能重新定義,也不能改變其值。</p><p>  抽象類中的變量默認(rèn)是friendly型,其值可以在子類中重新定義,也可以重新賦值。</p><p>  接口中的方法默認(rèn)都是public abstract類型的。</p><p><b>  結(jié)論</b></p><p>

溫馨提示

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

評論

0/150

提交評論