Detailed code for Java programming access control

  • 2021-01-03 20:55:28
  • OfStack

This paper focuses on the control of Java programming access rights, which is described as follows.

The modifier I didn't pay attention to before is not added in front of a general variable, one is not know what is useful, and one is lazy. When I encounter projects later, I will find that the difference between private and public is still very big.

(1) The first is the package name

When you use a class, such as a collection class, you need to introduce the package and then use the classes below the package. Such as:


package com.myown.iaiti; 
 
public class Print { 
  static void print(String s){ 
    System.out.println(s); 
  } 
} 

Custom packages, by introducing your own package, you can later print strings using your own method of writing. I have tried to set the path of my package to CLASSPATH before, so I can compile in another directory without putting the package in this directory, and it will run normally, otherwise I won't find the package of JAR.

(2)public

public, public, used to be used as a metaphor by Lao Tzu, this is a public thing, Lao Tzu uses it for you, public follows the data member, indicating that it is available for everyone.

(3)private

private, private, Laowei's private property, you do not touch it, except the package contains this data member of the class can be used, other classes can not be used, this part is the designer itself do not want to outsiders to see and modify.


public class Print { 
  public static void main(String[] args) { 
    GetIt g = new GetIt(); 
  } 
} 
 
 class GetIt{//  Pay attention to  1 A file is only available 1 A public class   If you want to  
   // prefix public  So you don't have alpha and beta Print Writing in the same 1 A file  
  private GetIt(){} 
} 

The constructor GetIt() is not visible, not visible, but useful, that's how the singleton works, controls the creation of objects.

Application of singleton pattern:


class A{ 
  private A(){}  
  private static A a = new A();  
  public static A getInstace(){ 
    return a; 
  } 
} 

Other classes cannot create objects with new because the constructor is decorated with private. The use is that When the A class is extremely complex and consumes memory, I need strict control over the creation of A objects. Since we have taken the singleton example, I will tell you the singleton by the way. I have seen some people write before, it is too classic, too great.

The above is the type of hungry Man, that is, 1 to help you new out, new will be used directly in the future, there is no thread problem, the disadvantage is that if it does not work, it is a waste of resources.

LanHanShi


public class A {     
   private A(){ }   
   
   private static A a;   
   public static A getInstance(){   
     if(a == null){   
       return a = new A();   
     }else{   
       return a;   
     }   
   }   
 } 

When using new to help you, null only new, but there is a thread problem, plus synchronized, but the efficiency is reduced, because of multithreading, one of them in use, will be occupied.

The perfect approach is to combine the two:


public class A { 
  //  Private static inner classes ,  Only if there are references ,  The class will be loaded  
  private static class LazyA { 
    public static A A = new A(); 
  } 
  public static A getInstance() { 
    return LazyA.A; 
  } 
} 

Don't understand the so-called before, a static inner class is used, both can delay loading, because only when used to new getInstace way, there is no multithreading problem because, static class belongs to all external class object, will only be loaded once, and after a static inner class instantiation, is the class level attributes, do not belong to an object, will only load, so as not to waste of resources, also does not have the problem of low efficiency of multithreading. The guy who came up with this is really good.

(4)protected

Print is the base class, Chinese people like to talk about the parent class, but foreigners think the subclass is more awesome. The base class is a foundation or foundation, protected is the property that Lao Zi left to his son.


public class Print { 
  protected void print(){} 
  private void cannotprint(){} 
} 
 
 
public class PrintSon extends Print{ 
  void get(){ 
    print(); 
    //cannotprint();  private  Private methods, subclasses still can't use them  
  } 
} 
 
 
public class NotSon { 
  void get() { 
    print(); 
  } 
} 

protected, subclasses can be obtained, between public and private.

(5) Access rights of class

There can be only one public class per file.

Class name and filename 1.

If there is no basis for singletons, it seems a little ahead of time, but after the knowledge points are improved, it will be easy to understand. The control of access rights depends on whether you want to give your part of the code to others to use directly.

conclusion

That's the end of this article on Java programming access control code details, I hope to help you. Interested friends can continue to refer to other related topics in this site, if there is any deficiency, welcome to comment out. Thank you for your support!


Related articles: