Java Programming Ideas (Second Edition) 5th chapter: Hide Implementation Details

Source: Internet
Author: User
Tags definition class definition empty interface client
Programming allows the change of things to be separated from the unchanging, so that the primary consideration of object-oriented design (OOD). Consider the version update of the library.

The concept of the component library (components library) and who is qualified to use the components such as Java is controlled by the keyword package and so on.

Use of the Import keyword, package keyword usage, default package: All files in the same directory, if there is no explicit package declaration, will be treated as the default package for that directory.

When you compile a small number of. java files, you can get a large number of. class files. The Java Jar Compression tool combines and compresses many. class files.

There is no conditional compilation mechanism similar to C + + in Java because Java considers itself cross-platform, and previous conditional compilation is designed to solve the C + + fast platform problem. However, conditional compilation also has the effect of debug, in order to better debug the program, you can introduce the original in C of the assertion mechanism (assertion), that is, the establishment of assertion class, and another definition of an empty assertion class into different namespaces, When you want to change the program code from the debug version to the shipping version, as long as in the original program to import the empty assertion class where the package can. This technique can be used on any type of conditional compiler code.

Java access modifier (Access specifiers): Public,protected,private should be placed before the definition of each member within the class, regardless of whether the member is a data member or function. The default access rights are interpreted as "friendly". That is, all other classes within the same package can access friendly members, but classes outside of package is private. Public acts as a role for the provider, while private means "Don't touch me." Protecked represents an inherited class that can be accessed.

For two reasons we need to control access rights: 1. To provide customers with a clear interface, delimit the boundaries. 2. Separate the interface from the implementation.

Class access rights: Java Access can also be used to determine "which classes in the library can be used by the library users," For classes access rights, you can control the client programmer or whether the library users have the right to produce a class object. However, there are some limitations
1. Each compilation unit (file) can have only one public class.
2. Although this is not usually done, the compile unit does not contain any public class. In this case, you can give any given file name. Note that you just take away the public keyword in front of the class definition, because the default is friendly, This means that other classes within the same package can Sheng objects of that class, but not package. However, if the class has a static public member, then the client programmer, even if the class object cannot be generated, Can still access this static member.
3.class cannot be private,protected. (except for implicit class inner class). If you do not want any other person to take a class, declare all of its constructors private so that you can prevent anyone from generating their objects. With two exceptions, the class static member can do it, Inheriting classes can also be done. Take a look at the following example:
Demonstrates class access specifiers. Make a class effectively private with private constructors

Class soup{
Private Soup () {}
public static Soup Makesoup () {
return new Soup ();
}
}
public class lunch{
void Test () {
Can ' t do this! Private constructor:
Soup priv1=new Soup ();
Soup Priv2=soup.makesoup ();
}
}
Class soup demonstrates how to declare all constructors as private to prevent a class object from being directly generated.
This is useful if you want to have some extra processing before you perform soup, or you want to record (or limit) How many soup objects are generated.






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.