Java Tour (7) Hide implementation & Reuse Class

Source: Internet
Author: User
Tags command line constructor control characters garbage collection implement inheritance tostring access
Hide Implementation & Reuse classes

Access control characters, import, each compilation unit (. java file) can only have one public class, and if there are other classes, then these classes become the
Auxiliary classes for the public class, because they are not public, so they are invisible to the outside world.

When compiling a. java file, each class in it produces output. The name of the file whose output is. java inside the class name, so it can't write
Several Java files produce a bunch of. class files, but don't be surprised that a Java file that works correctly is a bunch of. class files
, and can, of course, be encapsulated into a Java Archive (jar) file with jars. The Java interpreter is responsible for finding, loading, and interpreting these
of the file.

Package and import are like a sword and a shield. is to split a single global namespace so that no matter how many people on the Internet
With Java programming, you will never again encounter the problem of name conflict. It's worth noting that every time you create a package to name it, you also
A directory structure is implicitly set. This package must be kept in the directory indicated by its name, and this directory must be in the Classpath
Below.

Java access control character: Public is well known to anyone who can access it; Package permissions, default access rights, no keywords,
But it's often a package privilege (sometimes a friendly) that means that all classes that belong to this package can access this
Members, because the same compilation unit can only belong to one package, so the various classes in the same compilation unit, automatic access to each other.
protected, private.

Interface (interface) and implementation (Implementation):

A class can only be package and public, and if you don't want someone to access the class, you can set his constructor private so that no one can create the object of that class except you. And you can use a static method to create the object. (This is very useful in some situations)
There is also a way to implement similar functions, involving design patterns.

Reusable class

To use a class without altering the original code. There are two methods: Synthesis (compostion) and Inheritance (inheritance).

All non-primitive objects have a ToString () method that the compiler automatically invokes when the compiler needs a string and it is an object. So when the compiler sees in the "source=" +source that you want to add a string to the same non-numeric string, it says "because a string can only be added to a string, so I want to call the source's ToString ()." Because it's the only way to convert it into a string!. ”。 So it connects the two strings and returns the result to System.out.println () in string form. If you want to write a class that also has this function, just write a ToString () method.

You can create a main () for each class, and this is also a recommended programming method, because the test code can be put into the class. Even if a program includes many classes, it will only call the main () method of the class that you give at the command line. (As long as main () is public, it doesn't matter if the class is public.) So, when you type in javadetergent, it calls
Detergent.main (). Although cleanser is not public, you can also use Javacleanser to invoke Cleanser.main (). This practice of putting a main () into each class makes it easier to unit test the class. After testing, you do not have to remove main (), leaving it available for later testing.

Key words: Super, extends

The initialization of a derived class begins with the initialization of the base class, and if it is the default constructor, you do not have to deal with any problems, but if the constructor of the parent class is a constructor with parameters, then you need to manually use super to invoke the constructor of the parent class. Also, for a derived class constructor, calling the constructor of the base class should be the first thing it does.

In many cases, cleaning is not a problem; leave it to the garbage collector. But if you have to do it yourself, you have to work harder and be extra careful, because no one can help you in garbage collection. The garbage collector may never start. Even if it starts, you can't control the order in which it is recycled. It is best not to rely on the garbage collector to do anything unrelated to memory recycling. If you want to clean up, be sure to write your own cleaning methods

If a Java base class has a method that is overloaded several times, redefining that method in a derived class does not obscure any of the definitions in the base class (this is different from C + +). In fact, it is too common for a derived class to overwrite a method with the same argument list and the same return type.

Inheritance or synthesis?? In general, the synthesis is used for new classes to use the functionality of the old class, rather than the context of its interface. In other words, the object is embedded and used to implement the functionality of the new class, but the user sees the interface of the new class, not the object that is embedded in it. So, you have to embed in the new class
The old class object of private. Inheritance is the transformation of an existing class to obtain a special version. In short, you want to transform a more abstract class into a class that can be applied to specific requirements. A little thought will know that using vehicle (vehicle) object to synthesize a car (sedan) is meaningless--car does not contain vehicle, it is vehicle. Inheritance is to express a "Yes (is-a)" relationship, and the synthetic expression is to express "there
(has-a) "relationship.


To be Continued ...




Related Article

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.