1. The class must document precisely the effects of overriding any method. In other words, the class must document its self-use of overridable methods.
2. By convention, a method that invokes overridable methods contains a description of these invocations at the end of its documentation comment. The description begins with the phrase “This implementation”. This phrase should not be taken to indicate that the behavior may change from release to release.
3. To allow programmers to write efficient subclasses without undue pain, a class may have to provide hooks into its internal workings in the form of judiciously chosen protected methods or, in rare instances, protected fields. You should expose as few protected members as possible, because each one represents a commitment to an implementation detail.
4. The only way to test a class designed for inheritance is to write subclasses. Experience shows that three subclasses are usually sufficient to test an extendable class. One or more of these subclasses should be written by someone other than the superclass author.
5. Constructors must not invoke overridable methods, directly or indirectly. If you violate this rule, program failure will result. The superclass constructor runs before the subclass constructor, so the overriding method in the subclass will get invoked before the subclass constructor has run. If the overriding method depends on any initialization performed by the subclass constructor, the method will not behave as expected.
6. If you decide to implement Cloneable or Serializable in a class designed for inheritance, you should be aware that because the clone and readObject methods behave a lot like constructors, a similar restriction applies: neither clone nor readObject may invoke an overridable method, directly or indirectly. In the case of the readObject method, the overriding method will run before the subclass’s state has been deserialized. In the case of the clone method, the overriding method will run before the subclass’s clone method has a chance to fix the clone’s state. In either case, a program failure is likely to follow. In the case of clone, the failure can damage the original object as well as the clone. This can happen, for example, if the overriding method assumes it is modifying the clone’s copy of the object’s deep structure, but the copy hasn’t been made yet.
7. If you decide to implement Serializable in a class designed for inheritance and the class has a readResolve or writeReplace method, you must make the readResolve or writeReplace method protected rather than private. If these methods are private, they will be silently ignored by subclasses.
8. Prohibit subclassing in classes that are not designed and documented to be safely subclassed. There are two ways to prohibit subclassing. The easier of the two is to declare the class final. The alternative is to make all the constructors private or package-private and to add public static factories in place of the constructors.
9. You can eliminate a class’s self-use of overridable methods mechanically, without changing its behavior. Move the body of each overridable method to a private “helper method” and have each overridable method invoke its private helper method. Then replace each self-use of an overridable method with a direct invocation of the overridable method’s private helper method.
相关推荐
Item 19: Design and document for inheritance or else prohibit it Item 20: Prefer interfaces to abstract classes Item 21: Design interfaces for posterity Item 22: Use interfaces only to define types ...
> Design and document for inheritance or else prohibit it > > – Item 19, Effective Java, Third Edition Usage Java creates new types as open by default which can be dangerous. This checker ensures ...
Type inheritance is that phenomenon according to which we can say, for example, that every square is also a rectangle, and so properties that apply to rectangles in general apply to squares in ...
Object Pascal Handbook This is the book summary Table of Contents: Part I Chapter 1: Coding in Pascal Chapter 2: Variables and Data Types ...Chapter 17: The TObject Class Chapter 18: RunTime Library
Item 17: Implement the Standard Dispose Pattern 98 Item 18: Distinguish Between Value Types and Reference Types 104 Item 19: Ensure That 0 Is a Valid State for Value Types 110 Item 20: Prefer ...
NULL 博文链接:https://copperfield.iteye.com/blog/1140058
Most important, basing inheritance on JavaScript's prototypal chain is possible, but is not the best choice for prototypal inheritance or classical inheritance. What You'll Learn What are objects, ...
Most important, basing inheritance on JavaScript’s prototypal chain is possible, but is not the best choice for prototypal inheritance or classical inheritance. What You’ll Learn What are objects, ...
Chapter 17: Processes, AppDomains, and Object Contexts Chapter 18: Understanding CIL and the Role of Dynamic Assemblies Part VI: Introducing the .NET Base Class Libraries Chapter 19: Multithreaded, ...
Each chapter that follows describes one of twenty-three design patterns, recommends when to use it, and explains the impact that it will have on the larger design. The use of every pattern is ...
Chapter 17: Working with Dialogs and Controls Chapter 18: Storing and Printing Documents Chapter 19: Writing Your Own DLLs Chapter 20: Connecting to Data Sources Chapter 21: Updating Data Sources ...
软件设计课件:Lecture 08 CPP Inheritance.ppt
It is completely platform independent in that tests built for one browser or mobile device, will also work on all other browsers and mobile devices. Selenium supports all major development languages ...
Chapter 17: Threading Chapter 18: Graphical User Interface) Chapter 19: Graphical User Interface) Chapter 18: Event Handling Chapter 19: JDBC Programming Chapter 20: Exception Handling Chapter 21: ...
2.12.1. and, or, and not 2.13. while Loops and Recursion 2.13.1. while 2.13.2. car, cdr, cons: Fundamental Functions 2.13.3. while, Continued 2.13.4. Recursion 2.14. Macros 2.14.1. The list ...
Chapter 17: Writing Efficient C++ Chapter 18: Developing Cross-Platform and Cross-Language Applications Chapter 19: Becoming Adept at Testing Chapter 20: Conquering Debugging Part V: Using ...
Chapter 17: Reflection, Attributes, and Dynamic Programming Chapter 18: Multithreading Chapter 19: Synchronization and More Multithreading Patterns Chapter 20: Platform Interoperability and Unsafe ...
computer science for JavaScript rather than for the more typical Java or C++. Because JavaScript follows the prototypal inheritance pattern, unlike Java and C++ (which follow the inheritance pattern),...
HOUR 17: Using Polymorphism and Derived Classes HOUR 18: Making Use of Advanced Polymorphism Part VI: Special Topics HOUR 19: Storing Information in Linked Lists HOUR 20: Using Special Classes, ...
However, because the book is targeted for second- and third-year students, it is necessary to fill in the background as needed. To the extent possible without compromising correctness, the ...