1,The class names in different packages can be the same. Package is both a naming mechanism and a visibility control mechanism. You can define classes that cannot be accessed by code outside the package in the package, or define class members that can only be accessed by other Members in the same package, while other parts of the program cannot be accessed.
2,Java uses the file system directory to store packages. The. Class file must be stored in the directory of the corresponding package name, and the name must be exactly matched. Change the directory when renaming. You can create hierarchical packages. Package pkg1 [. pkg2 [. pkg3], such as package java. AWT. image.
How to find a package?First, the current working directory is used as the starting point by default, and the directory or path can be specified by setting the classpath environment variable. Finally, the-classpath option can be used for Java and javac, specify a path for the class.Note: The class path cannot contain the package itself.When executing the. Class file in the command line, you must add the package name, such as Java packagea. classa. You cannot directly use Java classa.
3. access protection.
|
Private |
No access modifier |
Protected |
Public |
| Visible in the same class |
Yes |
Yes |
Yes |
Yes |
| Same as Steamed Stuffed Bun visible |
No |
Yes |
Yes |
Yes |
| Non-subclass visibility of the same package |
No |
Yes |
Yes |
Yes |
| Visible for different steamed buns |
No |
No |
Yes |
Yes |
| Non-subclass visibility for different packages |
No |
No |
No |
Yes |
Class member access
Non-nested classes have only two access levels: Default and public. By default, this class can only be accessed by other classes in the same package.
If a class is public, the class is declared in the file.UniquePublic class, and the file name is the same as the name of this class.
4. Import the package.
Import pkg1 [. pkg2]. (classname | *); an asterisk indicates the entire package is imported. The compiler implicitly imports the java. lang Package for all programs.
If you use the * number to import two different packages, but the two packages have the same class, the compiler will not prompt. When the view uses this class, the compiler will prompt that an error occurs during compilation, the class in the package must be explicitly named.
Import is optional. You can use a fully qualified name to include the entire package level of the class. For example, Class A extendsJava. util.Date {...}.
5. interfaces.
Keyword interface, which can completely abstract the class interface from the implementation of the class. Dynamic Identification during runtime. Avoid bloated sub-classes. Specifies which jobs must be executed by the class, but cannot be performed now.
No instance variables and no method body. Once an interface is defined, any number of classes can implement the interface. A class can implement any number of interfaces.
Access interface name {
Return-type method_name1 (parameter-list); // abstract Method
Return-type method_name2 (parameter-list );
Return-type method_namen (parameter-list );
Type final-varname1 = value; // declarative variables, implicitly identified as final and static, must be initialized and the class cannot be modified.
Type final-varname2 = value;
Type final-varnamen = value;
}
If no access modifier is provided, the default access level is used, andOnly other members in the declared interface packageTo access the interface; If declared as public, all can be used.Cannot be declared as private or protected.
Each class containing the interface must implementAllMethod.
All methods and variables are implicitly declared as public.
You can use interfaces to import shared constants to multiple classes. The specific method is to declare interfaces containing variables and initialize these variables. When the class contains this type of interface, all these variables will be used as constants in the re-scope.
The interface can use the keyword extendsInherit another interface. If the interface implemented by the class inherits from another interfaceAllMethod.
Nested interface.You can declare an interface as a member of a class or another interface. Can be declared as public, private, or protected. Unlike top-level interfaces, top-level interfaces are either public or default access levels.
6. Implementation interface.
Class classname [extends superclass] [implements interface [, interface...] {}// inherits multiple interfaces.
If the two Implemented interfaces are clearSame Method, You can use this method for both interfaces.
The method to implement the interface must be declared as public, and the type signature (form parameter) of the method must be exactly matched.
Other members can be defined.
Access through interface reference. Variables can be declared as using interfaces rather than Class Object references. Dynamic query execution methods during running can cause serious burden, so do not use interfaces at will. For example, public static main (string ARGs []) {interface I = new implement (); I. func (...);}In this case, I cannot access any other member in the implement class (note ).Only the methods declared by the interface.
When the class does not implement all methods defined by the interface, the class must be declared as abstract.
On February 10