Summary of "Effect Java"

Source: Internet
Author: User
Tags protected constructor

Directory:I. Creating and Destroying objects (1 ~ 7)second, a common method for all objects (8 ~)Iii. Classes and Interfaces (~)iv. generic type (at ~)V. Enumeration and annotations (in the five ~ PNS)Vi. methods (from ~ to)VII. General Program Design (*)Eight, abnormal (~ +)ix. concurrency (~ ~)10. Serialization (~ +)Text:Chapter One: Creating and destroying Objects1, consider the static factory method instead of the constructor excellent: ① has a name ② do not have to create an object each time they are called ③ can be put back to the original return type of any subtype of the object ④ when creating a parameterized type instance, the code is more concise missing: ① class cannot be overridden if it does not contain a public or protected constructor ② No significant difference from other general static methods 2. When encountering multiple constructor parameters, consider using the constructor 3, using a private constructor or enumeration type to harden the Singleton property 4, hardening the ability to be non-instantiated through a private constructor 5, avoid creating unnecessary object 6, eliminate expired object references 7, Avoid using the finalization methodChapter Two: Methods that are common to all objects8. When overriding equals, please observe the general Conventions ① Each instance of the class is inherently unique ② does not care whether the class provides "logical equality" test functionality ③ the superclass has overridden equals, the behavior inherited from the superclass is also appropriate for subclasses ④ class is private or package-level private, you can determine its The Equals method is never called to implement the high-quality equals method trick: ① uses the "= =" operator to check if "parameter is a reference to this object" ② use the instanceof operator to check if the parameter is the correct type ③ convert the parameter to the correct type ④ for each "off" in the class Key field, check that the field in the parameter matches the corresponding domain in the object ⑤ when you write the Equals method, you should determine whether it satisfies the symmetry, transitivity, consistency
      • Overwrite equals when always overwrite Hashcode
      • Do not attempt to make the Equals method too intelligent
      • Do not replace object objects in the equals declaration with other types
9, overwrite equals always to cover hashCode10, always to cover toString11, carefully cover clone12, consider implementing comparable interfaceChapter III: Classes and Interfaces13. Minimizing the accessibility of classes and members 14, using access methods in public classes instead of public domain 15, making variability minimized ① do not provide any method that modifies the state of the object ② guarantee that the class will not be extended ③ make all domains final ④ make all domains private ⑤ Ensure mutually exclusive access to any mutable component 16, compound takes precedence over inheritance in short, inheritance is very powerful, but there are many problems, because it violates the encapsulation principle. It is only appropriate to use inheritance when there is indeed a subclass type relationship between the subclass and the superclass. Even so, if subclasses and superclass are in different packages, and the superclass is not designed for inheritance, then inheritance will lead to fragility. To avoid this, it is possible to replace inheritance with a composite and forwarding mechanism, especially when there is a suitable interface to implement the wrapper class. The wrapper class is not only more robust than subclasses, but also more powerful. 17, either design for inheritance, and provide documentation, or to prohibit inheritance 18, interface better than abstract class ① existing classes can easily be updated to implement the new interface ② interface definition of the ideal choice ③ interface allows us to construct a non-hierarchical type framework interface is often the best way to define types that allow multiple implementations. There is an exception to this rule, when the ease of evolution is more important than flexibility and functionality. In this case, you should use an abstract class to define the type, but only if you understand and accept these limitations. If you export an important interface, you should strongly consider providing a skeleton implementation class at the same time. Finally, you should design all of the public interfaces as carefully as possible and test them thoroughly by writing multiple implementation classes. 19. Interfaces used only to define type interfaces should only be used to define types, and they should not be used to export constants. 20, the class level is better than the label class tag class is rarely applicable when. When you want to write a class that contains a display label field, you should consider whether the tag can be canceled and whether the class can be replaced with a class hierarchy. When you encounter an existing class that contains a label field, consider refactoring it into a hierarchy. 21. Use the function object to represent the policy to declare an interface to represent the policy, and to declare a class that implements the interface for each specific policy. When a specific policy is used only once, an anonymous class is usually implemented to declare and instantiate the specific policy class. When a specific policy class is designed to be reused, its class is typically implemented as a private static member class, and is exported through the public static final domain, which is of the type of the policy interface. 22, priority static member class static member class, non-static member class, anonymous class, local class ① if a nested class needs to be visible outside of a single method, or if he is too long and not suitable for internal use, the member class should be used. ② if each instance of a member class needs a reference to its perimeter instance, itThe member class is made non-static, otherwise it is made static. ③ assumes that this nested class belongs to the interior of a method, and if you only need to create an instance in one place, and you already have a preset type that can describe the characteristics of the class, make it an anonymous class, or else it will be a partial class.  fourth Chapter generic type23. Please do not use the original ecological type 24 in the new code, eliminate the non-inspected warning each warning indicates that the classcastexception exception may be thrown at runtime slices. Do your best to eliminate these warnings. If you cannot eliminate a non-inspected warning, and you can prove that the code that caused the warning is type-safe, you can suppress the warning with a @SuppressWarnings ("unchecked") comment in the smallest possible range. Note the reasons for preventing the warning from being recorded. 25.

Summary of "Effect Java"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.