【正文】
結(jié)論 abstract class 和 interface 是 Java 語言中的兩種定義抽象類的方式,它們之間有很大的相似性。 3. abstract class 和 interface 所反映出的設計理念不同。我們該如何來設計、實現(xiàn)來明確的反映出我們的意思呢?前面已經(jīng)說過,abstract class 在 Java 語言中表示一種繼承關系,而繼承關系在本質(zhì)上是 isa關系。這樣引起的一個問題是那些僅僅依賴于 Door 這個概念的模塊會因為 報警器 這個概念的改變(比如:修改 alarm 方法的參數(shù))而改變,反之依然。對于 interface 來說則不然,并不要求 interface 的實現(xiàn)者和 interface定義在概念本質(zhì)上是一致的,僅僅是實現(xiàn)了 interface 定義的契約而已。因為如果后來想修改類的界面(一般通過 abstract class 或 interface來表示)以適應新的情況(比如,添加新的方法或者給已用的方法中添加新的參數(shù))時,就會非常的麻煩,可能要花費 很多的時間(對于派生類很多的情況,尤為如此)。 從編程的角度來看, abstract class 和 interface 都可以用來實現(xiàn) design by contract 的思想。我們可以構(gòu)造出一個固定的一組行為的抽象描述,但是這組行為卻能夠有任意個可能的具體實現(xiàn)方式。其實,兩者之 間還是有很大的區(qū)別的,對于它們的選擇甚至反映出對于問題領域本質(zhì)的理解、對于設計意圖的理解是否正確、合理。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 scaleup 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 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 schemes for analysis. This method violated the objectoriented 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 scaleup defined。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 ch