"Effective Java" reading notes series

zhaozj2021-02-16  89

More and more discovered this is a rare good book, Java programmers don't see this book is very regrettable. This chapter tells about classes and interface-related issues. These items are very important.

Item 12: minimizes the accessibility of classes and members

A good module design should be the most likely to encapsulate your internal information, which can minimize the degree of coupling between the modules. Developed in parallel, this will be accelerated, which will speed up the system to maintain. This issue is solved by accessing the control in Java.

Public means that this class is available in any range. Protected Indicates that only subclasses and classes can use private-package (default) to indicate that only private can be used in the package.

You should be as designed in 4321 when designing a class. If a class is just used by another class, you should consider designing it into the internal class of this class. Usually the class of public public should have a PUBLIC product, but we usually use a class to define all constants, which is allowed. However, it is necessary to ensure that these fields are either basic data type either to reference the pointing objects that cannot be modified. Otherwise they will mobilize. For example, in the definition below is unreasonable, there is no problem in the back. Public class con {public static final int [] data = {1, 2, 3}; // it is bad public static final string hello = "world"; public static final int = 1;}

Item 13: The unmodified class is more favored

Invoible class is meaning that they will not change, such as String classes. Their design is very convenient and high - they are threads. There are several rules for designing cannot modify classes:

Do not provide any way to modify the object to ensure that there is no way to be overwritten, you can design it to Final all fields to be designed as a Final All fields to make sure that the externally cannot access the modified components of the class cannot be modified. There is a disadvantage. Creating different objects when creating a different class, String is like this. Usually some solution is to provide a help class to make up, such as StringBuffer classes.

Item 14: The combination is more worth considering than inheritance

The most important way to realize code reusing is to inherit, but inherit is destroyed, causing the software's key mass. If the subclass inherits the parent class, it relies on the parent class from the method of inheriting from the parent class, once he changed the unpredictable result. The author introduces InstrumentHashSet as an alternative, the reason is that there is no understanding of the method of the parent class. The solution given by authors is to solve problems with packaging and forwarding methods by ginching instead of inheritance. Take the class that wants to extend as a private factory for this class. Pass the method parameters to this member variable and get the return value. The disadvantage of this is that such a class is not suitable for returning to the frame. Although inherited, we should not abuse, only we can determine that they are IS-A to have relationships.

Item 15: If you want to use inheritance, you must have quality assurance, otherwise don't use it.

In order to avoid the issuance of inheritance, you must provide accurate documentation to explain the problem that the relevant method may occur. Do not call the method that can be overwritten in the constructor, because the problem occurs when the subclass coverage method. Import java.util. *;

Public class subclass extends superclass {private final date; public subclass () {date = new date ();} public void m () {system.out.println (date);} public static void main (String [] args {Subclass S = New Subclass (); SM ();}} Class Superclass {m ();} public void m () {}} Since Date is initialized, Super () has been called, So the first output NULL instead of the current time. Because of the functionality of the constructor when clone () or serialization, the readObject () and clone () methods are preferably not included in the method.

Item 16: Priority between interfaces and abstract classes

Interfaces and abstract classes are used to achieve polymorphism, but we should give priority to the interface. do you know? James said if he wants him to redesign Java, he will design all the interface. The advantage of an abstract class is to expand because it is inherited, and the method can be implemented within an abstract class, and the interface is not.

Item 17: Interface should only be used to define types

The interface can be used for the Collection c = new xxxx (); this is our most common. Do not use interfaces to do other things, such as definitions of constants. You should define a class, which contains the Public Final Static field.

Item 18: Choosing the former between static and non-static internal classes

If a class is defined in other classes, it is a nested class, which can be divided into static internal classes, non-static internal classes, and anonymous classes. Static Member Class is the purpose of which is to serve Enclosing Class, if there are other purposes, you should design it into top-level classes. NonStatic Member Class is associated with Enclosing Class Instance, if you don't need to access Enclosing Class Instance, you should design it into static, or you will waste time and space. Anonymous Class is carried out simultaneously declared and initialization. Can be placed in any location of the code. Typical applications are Listener and Process Object such as Thread.

转载请注明原文地址:https://www.9cbs.com/read-18586.html

New Post(0)