Beginning Java Objects

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没有类的多继承,多继承很复杂的一点,如果两个父类都有相同的方法,子类怎么处理?

你可能感兴趣的:(Beginning Java Objects)