Beginning Java Objects
Aggregation is a special form of association, alternatively referred to as the “consists of”, “is composed of”, or “has a” relationship.Like an association, an aggregation is used to represent a relationship between two classes, A and B. But, with an aggregation, we’re representing more than mere relationship: we’re stating that an object belonging to class A, known as an aggregate,is composed of, or contains, component objects belonging to class B.
Note that these aggregation statements appear very similar to associations, where the name of the association just so happens to be is composed of or contains. That’s because an aggregation is an association in the broad sense of the term(aggregation 是association的一種特殊表現形式)!aggregation 和associations UML表現不同但最終代碼表現形式一樣
Composition is a strong form of aggregation, in which the “parts” cannot exist without the “whole.” As an example, given the relationship “a Book is composed of many Chapters”, we could argue that a chapter cannot exist if the book to which it belongs ceases to exist; whereas given the relationship “a Car is composed of many Wheels”, we know that a wheel can be removed from a car and still serve a useful purpose. Thus, we’d categorize the Book–Chapter relationship as composition and the Car–Wheel relationship as aggregation.
繼承沒留意的好處:
• Best of all, we can derive a new class from an existing class even if we don’t own the source code for the latter! As long as we have the compiled bytecode version of a class, the inheritance mechanism works just fine; we don’t need the original source code of a class in order to extend it. This is one of the most dramatic ways to achieve productivity with an objectoriented language: find a class (either one written by someone else or one that is built into the language) that does much of what you need, and create a subclass of that class,adding just those features that you need for your own purposes.
• classification is the natural way that humans organize information; so, it only makes sense that we’d organize software along the same lines, making it much more intuitive and hence easier to develop, maintain,extend, and communicate with users about.
繼承與Association, aggregation異同(P186):
Association, aggregation, and inheritance are all said to be relationships between classes. Where inheritance differs from association and aggregation is at the object level.inheritance is indeed a relationship between classes, but not between distinct objects.
注意:避免連鎖反應,Whenever possible, avoid adding features to non-leaf classes once they have been established in code form in an application, to avoid ripple effects throughout an inheritance hierarchy. 說比做容易,這就要求在編碼之前盡可多的花時間在需求分析和對象建模階段,雖然不能避免新需求出現,但至少避免忽視遺漏了當前的需求。
Overriding:子類繼承父類,重寫唯一能改變的是方法的訪問控制,而且只能比父類更寬松,如父類用的是private,子類可以用public。參考了下thinking in java 4th P202 發現這種說法不對,而且是一個陷阱:父類的該方法根本對子類不可見!子類的該方法實際上是一個全新的方法,連重載都算不上。所以只能重寫non-private方法。遇到private方法你得小心,沒有編譯錯誤,但不會像你想象的工作,最好給方法重新取名,避免陷阱。
不要做的事情:
We shouldn’t change the semantics—that is, the intention, or meaning—of a feature.For example:
• If the print method of a superclass such as Student is intended to display the values of all of an object’s attributes in the command window, then the print method of a subclass such as GraduateStudent shouldn’t, for example, be overridden so that it directs all of its output to a file instead.
• If the name attribute of a superclass such as Person is intended to store a person’s name in “last name, first name” order, then the name attribute of a subclass such as Student should be used in the same fashion.
We can’t physically eliminate features, nor should we effectively eliminate them by ignoring them. To attempt to do so would break the spirit of the “is a” hierarchy. By definition, inheritance requires that all features of all ancestors of a class A must also apply to class A itself in order for A to truly be a proper subclass. If a GraduateStudent could eliminate the degreeSought attribute that it inherits from Student, for example, is a GraduateStudent really a Student after all? Strictly speaking, the answer is no.
進一步從實用角度說,如果我們重寫一個方法但不在這方法里做任何事情,其他繼承我們類的人就會出問題:他們覺得我們的方法是有意義的(特別是他們不能看到我們源代碼的時候)。而我們則打破了“is a” 原則,所以絕不要這樣做!
protected關鍵字的運用,用于控制繼承的訪問控制。
運用super(arguments) 減少子類構造函數重復父類構造函數代碼,和this類似必須在構造函數最開始調用。
Student s = new Student("Fred", "123-45-6789"); 執行這段代碼,Object的構造函數會自動執行,接著Student 的父類Person構造函數仔細,然后是我們調用的Student構造函數,如果調用的Student構造函數沒有顯示調用父類構造函數,則相當于默認調用super() 。
java沒有類的多繼承,多繼承很復雜的一點,如果兩個父類都有相同的方法,子類怎么處理?
Note that these aggregation statements appear very similar to associations, where the name of the association just so happens to be is composed of or contains. That’s because an aggregation is an association in the broad sense of the term(aggregation 是association的一種特殊表現形式)!aggregation 和associations UML表現不同但最終代碼表現形式一樣
Composition is a strong form of aggregation, in which the “parts” cannot exist without the “whole.” As an example, given the relationship “a Book is composed of many Chapters”, we could argue that a chapter cannot exist if the book to which it belongs ceases to exist; whereas given the relationship “a Car is composed of many Wheels”, we know that a wheel can be removed from a car and still serve a useful purpose. Thus, we’d categorize the Book–Chapter relationship as composition and the Car–Wheel relationship as aggregation.
繼承沒留意的好處:
• Best of all, we can derive a new class from an existing class even if we don’t own the source code for the latter! As long as we have the compiled bytecode version of a class, the inheritance mechanism works just fine; we don’t need the original source code of a class in order to extend it. This is one of the most dramatic ways to achieve productivity with an objectoriented language: find a class (either one written by someone else or one that is built into the language) that does much of what you need, and create a subclass of that class,adding just those features that you need for your own purposes.
• classification is the natural way that humans organize information; so, it only makes sense that we’d organize software along the same lines, making it much more intuitive and hence easier to develop, maintain,extend, and communicate with users about.
繼承與Association, aggregation異同(P186):
Association, aggregation, and inheritance are all said to be relationships between classes. Where inheritance differs from association and aggregation is at the object level.inheritance is indeed a relationship between classes, but not between distinct objects.
注意:避免連鎖反應,Whenever possible, avoid adding features to non-leaf classes once they have been established in code form in an application, to avoid ripple effects throughout an inheritance hierarchy. 說比做容易,這就要求在編碼之前盡可多的花時間在需求分析和對象建模階段,雖然不能避免新需求出現,但至少避免忽視遺漏了當前的需求。
Overriding:子類繼承父類,重寫唯一能改變的是方法的訪問控制,而且只能比父類更寬松,如父類用的是private,子類可以用public。參考了下thinking in java 4th P202 發現這種說法不對,而且是一個陷阱:父類的該方法根本對子類不可見!子類的該方法實際上是一個全新的方法,連重載都算不上。所以只能重寫non-private方法。遇到private方法你得小心,沒有編譯錯誤,但不會像你想象的工作,最好給方法重新取名,避免陷阱。
不要做的事情:
We shouldn’t change the semantics—that is, the intention, or meaning—of a feature.For example:
• If the print method of a superclass such as Student is intended to display the values of all of an object’s attributes in the command window, then the print method of a subclass such as GraduateStudent shouldn’t, for example, be overridden so that it directs all of its output to a file instead.
• If the name attribute of a superclass such as Person is intended to store a person’s name in “last name, first name” order, then the name attribute of a subclass such as Student should be used in the same fashion.
We can’t physically eliminate features, nor should we effectively eliminate them by ignoring them. To attempt to do so would break the spirit of the “is a” hierarchy. By definition, inheritance requires that all features of all ancestors of a class A must also apply to class A itself in order for A to truly be a proper subclass. If a GraduateStudent could eliminate the degreeSought attribute that it inherits from Student, for example, is a GraduateStudent really a Student after all? Strictly speaking, the answer is no.
進一步從實用角度說,如果我們重寫一個方法但不在這方法里做任何事情,其他繼承我們類的人就會出問題:他們覺得我們的方法是有意義的(特別是他們不能看到我們源代碼的時候)。而我們則打破了“is a” 原則,所以絕不要這樣做!
protected關鍵字的運用,用于控制繼承的訪問控制。
運用super(arguments) 減少子類構造函數重復父類構造函數代碼,和this類似必須在構造函數最開始調用。
Student s = new Student("Fred", "123-45-6789"); 執行這段代碼,Object的構造函數會自動執行,接著Student 的父類Person構造函數仔細,然后是我們調用的Student構造函數,如果調用的Student構造函數沒有顯示調用父類構造函數,則相當于默認調用super() 。
java沒有類的多繼承,多繼承很復雜的一點,如果兩個父類都有相同的方法,子類怎么處理?
posted on 2009-12-08 16:57 yuxh 閱讀(251) 評論(0) 編輯 收藏 所屬分類: jdk 、OO設計