Java journey (7) Hidden Implementation & reuse class

Source: Internet
Author: User

Hidden Implementation & reuse class

The Access Controller, import, and each compilation unit (. Java file) can only have one public class. If there are other classes, these classes will become
The auxiliary classes of the public class, because they are not public, so they are invisible to the external world.

When compiling a. Java file, every class in it will generate an output. The output file name is the name of the class in. Java, which cannot be written.
Several java files will generate a bunch of. class files, but don't be surprised. A working Java file is originally composed of a bunch of. class files.
Of course, you can also use jar to encapsulate them into Java archive (jar) files. The Java interpreter is responsible for searching, loading, and interpreting these
File.

Package and import are like a knife and a shield. Is to separate a separate global namespace, no matter how many people on the Internet
If you use Java for programming, you will no longer encounter name conflicts. It is worth noting that every time you create a package to name it, you also
Implicitly sets a directory structure. This package must be saved in the directory indicated by its name, and this directory must be in the classpath
Below.

Java Access Controller: public is well known to be accessible to anyone; Package permission, default access permission without keywords,
But usually it becomes the package permission (sometimes friendly), which means that all classes that belong to the same package can access this
Because the same compilation unit can only belong to the same package, each class in the same compilation unit can automatically access each other.
Protected, Private.

Interface and implementation ):

Classes can only be package and public. If you do not want others to access this class, you can set its constructor to private. In this way, apart from you, no one can create the class object. You can use a static method to create an object. (This is useful in some scenarios)
Another method for implementing similar functions involves the design mode.

Reusable class

Use the class without modifying the original code. There are two methods: compostion and inheritance ).

All non-primitive objects have a tostring () method. When the compiler needs a string but it is an object, the compiler automatically calls this method. So when the compiler shows from "Source =" + source that you want to add a string to the same non-string, it will say, "Because the string can only be added to the string, therefore, I want to call source tostring (), because only in this way can we convert it to string! ". Then it connects the two strings and returns the result to system. Out. println () in the string format (). If you want the class you write to have this function, you only need to write a tostring () method.

You can create a main () for each class, and this is also a recommended programming method, because in this way, the test code can be put into the class. Even if the program contains many classes, it will only call the main () method of the class you provided in the command line. (As long as main () is public, it doesn't matter if the class is public .) So when you enter javadetergent, it will call
Detergent. Main (). Although cleanser is not public, you can use javacleanser to call cleanser. Main (). This method of placing a main () in each class makes unit testing easier. After testing, you do not have to remove main (); leave it for future testing.

Keywords: Super, extends

The initialization of a derived class starts from the initialization of the base class. If both are Default constructors, you do not have to deal with any problems. However, if the parent class constructor is a constructor with parameters, you need to manually use super to call the constructor of the parent class. In addition, for a derived class constructor, calling the base class constructor should be the first thing it does.

In many cases, cleaning is not a problem; leave it to the garbage collector. But if you want to do it on your own, you have to work harder and be especially careful, because no one can help you with garbage collection. The Garbage Collector may never start. Even if it is started, you cannot control its recycling order. It is best not to rely on the garbage collector to do anything unrelated to memory collection. If you want to clean up, you must write the cleanup method yourself.

If there is a method that has been overloaded several times in the base class of Java, You can redefine the method in the derived class, it will not overwrite any defined in the base class (this is different from C ++ ). In fact, using the same parameter list and the same return type in a derived class overwrites the method is too common.

Inheritance or synthesis ?? In general, synthesis is used for new classes to use the functions of the old classes, rather than for their interfaces. That is to say, the object is embedded and used to implement the functions of the new class, but the user sees the interfaces of the new class, rather than the interfaces of the embedded object. Therefore, you have to embed it in the new class.
Private. Inheritance is to transform existing classes to get a special version. In short, you need to transform a more abstract class into a class suitable for certain requirements. If you think about it a bit, you will know that it is meaningless to use vehicle (vehicle) objects to synthesize a car (CAR). A car does not contain vehicle, which is originally vehicle. Inheritance needs to express a "is-a" relationship, while synthesis needs to express "yes ".
(Has-a) "link.

Not complete ......

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.