Introduction to programming thinking in Java Encapsulation
Think in Java chapter 5th hidden implementation process
From the first sentence, I feel that this chapter will make meUnsatisfied. "Separating changed things from unchanged things" has nothing to do with encapsulating a dime. Encapsulation changes, also known as OCP, are independent of the encapsulation of OOP.
Concerning the design of the Library (Class A), program B of the dependent database requires that the Library isBackward compatibility. Program B depends onA Interface. The creator of the database does not need to modify or improve the interface as needed.
Here, the interface of a indicates the service that a needs to provide. The designer cannot or will not consider whether the service (method name and parameter) will remain unchanged or changed in the future.
In JDK, the method used to change the class interface is as follows:
- List the APIS/methods of classes that do not support deprecated.
- The default method of Java 8 provides regret for changes to Java interfaces.
Encapsulation is an interface that forms a class and has nothing to do with other ideas.Java's "access indicator" concept is only used to restrict limited class interfaces. Of course, if all the things of your class are public-All interfaces, the consequence is indeed: the modification and improvement of your opponent's legal name and parameters cannot be moved ". The question is, why do you need to modify the method name and parameter-interface elements?
Implementation of modification methodsIt is the hope of the common library creator.
Package 5.1: Database Unit [the reason All this importingIs to provide a mechanic to manage name spaces.] It looks so awkward. The main reason for Java package introduction is to divide the namespace. The Import Statement (the Import/Import Statement) is just a lazy method and has no meaning. [So far, most examples of this book exist only in a single file and are designed for local (local) use without conflicting with the package name (in this case, class Name in "Default package ). This is an effective approach, and the rest of the book will also take into account the simplification of the problem. Use it whenever possible.], We recommend that you use bluej. The first example is not in the default package. The default package is good unless you use the console to learn Java and do not like troublesome directory conversion.
5.1.1 create a unique package nameThe environment variable classpath. [1. automatically compile as the first time an object is created for the imported class (or when a static member of a class is accessed), the compiler will find the same name in the appropriate directory.
. Class file (so if you create an object of Class X, it should be X. class ). If only X. Class is found, it is the class that must be used. However, if it finds an X. Java file in the same directory,
The compiler compares the date tags of two files. If X. Java is newer than X. Class, X. Java is automatically compiled to generate the latest X. Class.If a specific class is not found in the. Java file with the same name as the class, the class will be treated as above .] Oh ?!
5.1.2 custom tool Library1. classpath traps
5.1.3 use import to change behaviorsLet's see how to simulate the assertion mechanism. You can use classes with the same name in two packages by changing the imported package.
5.1.4 package disabling is useless.
5.2 Java access indicator 5.2.1 "friendly" -- vs. 6.2.3 package-level private
5.2.2 public: interface access
5.2.3 PRIVATE: cannot be used to optimize the private method by JVM; private constructor;
5.2.4 protected: "A friendly one. Fortunately, I mentioned inheritance in 2.1.1's replacement principles;
5.3 The separation of interfaces and implementation between interfaces and implementations is supposed to be explained. It turns out to be information hiding (his implementation hiding) and encapsulation. And
Class interface and class implementationIs this encapsulation? Class Interfaces
Why?Must be public -- [client programmers Can't do anythingBut send messages to the public interface], usually protected is considered as an interface. The following are irrelevant things, such as the class structure. I like it:
Public class XXXX {<domain declaration> <constructor, initialization block> <method definition> <internal class, internal interface>}[Class users can read from top to bottom], please, what he should read is
JavadocInstead of source code. 5.4 class access public class
This chapter gives 60 points.