1. Access control in Java
Table 1-1
Visible / Accession Public Protected Package Private In the same class YES YES YES YES in the same package YES YES YES NO Different packets in subclats of Yes No no no in the subclass of Yes Yes yes no no NO NO NO NO
Description:
(1) In Java, there is public, protected, private, three display modifiers for control visibility, Package is not a modifier, which is implied, that is, if the class, variables are not added before the display Sexual modifier, it is the package level. If no package is specified in the definition of the class, Java will put it in the default package, which generally said that this default package is the current directory.
(2) The method in the subclass If the method of the parent class is overloaded, then the visible level of the method should be more or the same, such as the method in the parent class is PUBLIC, and the method in the subclass must be public.
(3) In Java, in general, the variable member is preferably private, and access to their access can be used in these methods to ensure the consistency of data. These method names are typically prefixed as GET and SET.
2 bags
The package name is generally lowercase, and the first letter of the class name is generally uppercase, so when referenced, the package name and class name can be distinguished. If you do not use the package definition package name before the class is defined, then this class belongs to the default package.
2.1 Hello Package
First learn from the use of Package by a simple package definition:
Package testpackage;
Public Class Test
{
Public static void main (string args [])
{
Mnode node = new mnode ();
Node.Print ("Hello");
}
}
Description:
(1) Use the package keyword to specify the package where the class is located;
(2) Package statement must be the front of the file;
(3) Javac -d can be used when compiled. Test.java automatically generates a package needs a directory;
(4) You can use Java TestPackage.Test to perform compiled code;
Several effects of 2.2 packages:
(1) A better organizational class, the package is similar, and the folder can put different files in the same folder, and the package can also put different types of files in the same package;
(2) Reduce the conflict of the class name, which is similar to the folder, the files in the same folder cannot be renamed, the files in different files can be renamed, the class names in the same package cannot be repeated, the classes in the package Name can be repeated;
(3) A certain protective role in the package in the package, see Java's access control;
2.3 use of Import
(1) Direct reference to the designated class, such as Import Java.util.Vector;
(2) Reference multiple classes in a package, such as Import Java.awt. *. More specifically, it is not to reference all classes in java.awt, but only the class defined as public, and only references the class referenced by the code, so this reference method does not reduce the performance of the program; ) * Instead of class name, can not replace the package name, such as Import Java.awt. *, Only reference java.aw, not to reference the package under java.aw;
(4) Import java.awt.f *, this method is wrong;
(5) The Import statement is after the Package definition before all class definitions;
(6) IMPORT only tells the compiler and interpreter where to find classes, variables, methods definitions, and do not introduce these definitions into code;
2.4 Use of the package
There are several mechanisms that can use the classes in the package:
(1) If the class to be used is a java.lang package, you can directly use the class name to reference the specified classes, do not need to be added, because the package java.lang does not display the use of IMPORT, it is default Introduced;
(2) If the class to use is in other packages (except java.lang), then you can use the class name to quote this class, such as java.awt.font
(3) For classes that are often used (this class in other packages), it is preferred to use the import reference to the specified package, such as java.awt. *
(4) If the different packages introduced in Import include the same class name, then the use of these classes must be added to the package name.
(5) The interface can also belong to a package, or IMPORT can be used to introduce classes and interfaces in other packages.