Java programming ideas - talk about Java's access control

How to distinguish things that change from things that remain unchanged, that is, between front-end programmers (developers who complete some projects by combining classes and methods in various class libraries) and back-end programmers (class libraries) Developers) to draw a clear line between, requires a certain access control mechanism to complete. The control of access rights is often referred to as hiding with implementation . Wrapping data and methods into classes, and hiding with implementation, are often collectively referred to as encapsulation . What is encapsulated is actually a data type with characteristics and behavior . The main access control in Java is mainly implemented through the package mechanism and permission modifiers.

First, the package control mechanism of Java

1. Background

       In order to prevent class conflicts, Java proposes to pack one or more classes with the same function or meaning into a package (keyword package), one is to facilitate the management of these classes, and the other is to distinguish classes with the same name to prevent conflicts.

2. A few points to pay attention to in the package

      Each compilation unit (that is, a class file) can only have one public class (public class) visible to outsourcing . If there are multiple compilations, an error will be reported. If other classes are required to assist the main class, then It cannot be modified with public. Although there can be multiple classes in a .java file, there are multiple .class files generated by the Java compiler, that is, each class will generate a .class file named by the class name.

  • Creation of the package: The package should be placed at the top of each compilation unit (Java file) , indicating that this file is managed by this package. If you want to access the accessible content in the package, you need to refer to the package name. can do. In order to ensure the conflict and identification of the package name, the package is generally represented by the reverse domain name + [name] in all lowercase letters (because the domain name applied for each project must be unique, which can ensure the uniqueness of the package), but If there is no domain name, you can also use a multi-field combination to represent the package name, but you must use the name that is as non-repetitive as possible.

    Such as: com.practicepackage.util or name.sex.phone.hfdy.util

package com.practicepackage.util;

public class TestPackage{
}

  • Use of packages: There are two ways to access classes through packages -

     One is to call directly through the package name: (this method is redundant)

com.practicepackage.util.TestPackage t = new com.practicepackage.util.TestPackage();

     The second is to import through the import keyword: (This method is flexible and recommended)

import com.practicepackage.util.TestPackage;
class Test{
    public void usePackage(){
        TestPackage t=new TestPackage();
    }
}

When the package is created, the directory structure specified by the compiler is actually given to find the .java file and store the .class file.

2. Access modifiers in Java

       As mentioned before, there are three kinds of access modifiers in Java, according to the size of the access domain, they are: public, protected, private, but Java provides an access modifier in order to prevent programmers from forgetting to write access modifiers when writing programs. A default access permission, also known as package access permission. Just like its name, package access permission can only be accessed by classes in the same package. Next, we will introduce these four access permissions (in descending order restricted domain scope).

  • public : Just like the meaning of this word, whether classes, methods or member variables modified with this modifier are public resources for the real project, and any class can access these resources . So if you want front-end programmers to be free to use and change these resources, this modifier will be the best choice.
  • protected : The resource modified by this modifier can first be accessed by the class with the inheritance relationship, that is to say, if there is a class Aclass in the A package with a method " protected f(){} ", then there is another The class Bclass in the B package wants to access the f() method. The action that must be done to access the success is to establish an inheritance relationship between the two classes, that is, "Bclass extends Aclass" . In fact, protected also provides package access rights , which means that subclasses under the same package or under different packages can access protected resources in the parent class.
  • Default package access : Only classes within the same package can access resources with default access.
  • private : This modifier is very important in actual development because its access domain is very small , providing a guarantee for the stability and security of the system. Because a large part of resources should be hidden from client programmers , this is very helpful for subsequent jar package (class library) upgrades, because even if some private resources are modified, they are not available to client programmers. It will have a great impact, and it will not have a fatal impact on projects developed using this jar package.

       In addition, it should be noted that in daily development, classes cannot be modified with private and protected , but only with public and default access permissions . If you do not want other classes to have access to this class, you can use this class The constructor of the class is set to private. At this time, this class can be instantiated in two ways. First, the class can be instantiated by creating a static-modified method in this class, and then the external class can be modified by static. method to obtain an instance; second, it can be achieved through the singleton pattern as follows:

public class Test{
   private Test(){}
   // Method 1 to get the class instance
   public static Test useTest(){
     return new Test();
   }
   //Method 2 - singleton mode (only one object can be created in this method, because test is a private public resource)
   private static Test test=new Test();
   public static Test makeTest(){
    return test;
   }

}

     (It should also be noted that it is also a small programming skill: when writing a class, whether it is a member variable or a method, it is best to write it in the descending order of the authority domain, because the public modification is basically external. The interface, which is what front-end developers care about most, this way of writing is very readable.)

      The above four types of access modifiers have their own characteristics and play a very important role in actual development. To develop an excellent software project, programmers should carefully consider which resources need to use which access rights. Therefore, we must develop good programming habits, and must not go public all the way laughing out loud.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325889881&siteId=291194637