The consumer of the class library can use the changed and unchanged items separately.
When writing a Java source code file (compilation unit: One. java files), each compilation unit has only one public class, and the name is the same as the file name. Other classes on the compilation unit are mainly public class services and cannot be seen outside the package.
The way to obtain access to a member: 1. Set the Member to public. Access is allowed everywhere. 2. Without the permission modifier, place other classes in the same package and access through package access. 3. The inherited subclass can access the public protected of the parent class. 4. The get set method is provided.
public class Test {
private Test(){}
static Test makeATest(){
return new Test();
}
}
Private can be used to control how to create objects and prevent others from directly accessing a specific constructor. This example also has another effect, because the default constructor is a unique constructor and is private, it will hinder the inheritance of this class.
Contains the package access permission, and the inheritance class can also be accessed.
- Interfaces and Implementations
Access control is called the hiding of specific implementations. Package data and methods into the class and hide the specific implementation. This is called encapsulation. The result is a data type with characteristics and behaviors of a colleague. Access Permissions define the boundaries of permissions within the data type: 1. Set the boundaries between the client programmer and the client programmer. 2. Separate the specific implementation of interfaces. If the structure is used in a group of programs, the client programmer can only send information to the interface, then it can change everything that is not public.
Class cannot be private protected (internal class can be ). The class can be the package access permission or public. If you do not want others to have access permissions to the class, you can set all constructors to private.
Only one object of the class can be created.
public class Test {
private static Test test = new Test();
private Test(){}
static Test makeATest(){
return test ;
}
}
Import static com... classname. *; indicates importing static methods in this class. Of course, you can only import a static method. You just need to replace. * with the static method name. Then, in this class, you can directly call static methods with the method name, instead of using the classname. method name method.