Hide implementation & multiplex
Access Control, IMPORT, each compiler (.java file) can only have a public class, if there are other classes, these classes become auxiliary class for this public class, because they are not public, so For the outside world, they are not seen.
When compiling .java files, each class in it produces output. The file name of its output is the name of the .java, this can't write a few Java files will generate a bunch of .class files, but don't be strange, a Java file that can work properly is made up of a bunch of .class files Of course, we can also use JAR to encaps them into the Java Archive (JAR) file. The Java interpreter will be responsible for looking for, loading and interpret these files.
Package and Import are like a knife a shield. It is a separate global name space to split, so no matter how many people on the Internet use Java programming, you will not touch the problem of the name conflict. It is worth noting that when you create a package to give it a name, you also implied a directory structure. This package must be saved in the directory indicated by its name, and this directory must be under ClassPath.
Java Access Control: Public is well known to anyone can access; Package privileges, the default access is there is no keyword, but it is usually a package privilege, (sometimes it is friendly) it means all assignees This package can access this member, because the same compilation unit can only belong to one package, so the individual classes in the same compilation unit can be accessed. protected, private.
Interface and Implementation:
Class can only be Package and Public, if you don't want others to access this class, you can set his constructor into private, so that no one can create that class. And you can use a static method to create an object. (This tricks in some cases) also has a method of implementing similar functions involving design mode.
Multiplex
To use the class without changing the original code. There are two methods: compost and inheritance.
All non-Primitive objects have a toString () method, when the compiler needs a string and it is an object, the compiler will automatically call this method. So when the compiler sees from "Source =" Source, you want to add a string to the same non-string, it will say "Since String can only add String, I want to call Source Tostring (), because only this can be converted into string! ". So it connects these two String, then returns the results to System.Out.println () form. If you want to write the class you have this feature, just write a toString () method.
You can create MAIN (), and this is also a programming method worth advocating, because in this way, the test code can be placed in the class. Even if the program includes many classes, it will only call the main () method of the class you given in the command line. (As long as main () is Public, it is not that the class is not public, it is not important.) So when you enter javadetergent, it will call detergent.main (). Although Cleanser is not public, you can also use JavacLeanser to call cleanser.main (). This kind of main () approach to each class can make the class test make it easier. Once you have finished the test, you don't have to remove main (); leave it to be used later. Keywords: super, extends
The initialization of derived class is started from the initialization of the base class. If it is the default constructor, no problem is handled, but if the parent class's constructor is a constructor with parameters, you need to manually use the super to call the parent class. Constructor. In addition, the configuration function of calling the base class should be the first thing it does for the assignment function.
In many cases, the cleaning is not a problem; it will leave it to the garbage collector. But if you want to do it yourself, you can only have a bit hard, but also be extra care, because in terms of garbage collection, no one can help you. Garbage collectors may never start. Even if it starts, you can't control its recycling order. It is best not to rely on garbage collectors to do anything that is not related to memory recovery. If you want to clean up, you must write your own method.
If there is a method of being overloaded several times in Java's base class, then redefines that method in the derived class, it does not cover any one of the base classes (this is different from C ). In fact, in the derived class with the same parameter list, the same return type to override this method, it is too common.
Inheritance or synthesis? ? In general, synthesis for new classes to use the functions of the old class, rather than their interface. That is, the object is embedded, and it is used to implement the function of the new class, but the user sees the interface of the new class, not the interface of the object embedded. Therefore, you have to embed the old class object in the new class. Inheritance is to make a transformation of existing classes to get a special version. In short, you have to transform a more abstract class into a class that can apply to certain specific needs. I will know if I want to use a Vehicle (vehicle) object to synthesize a CAR (car) is meaningless -car does not contain Vehicle, which is a Vehicle. Inheriting is a "YES (IS-A)" relationship, while synthetic expression is to express "there is (HAS-A) relationship.
Sustainable continued. . . . . .