Encapsulation in Java
1, Scope of variable
(1) , member variables and local variables
Variable definition: different definition positions of variables will lead to different functions of variables.
1. Member variable: defined inside the class and outside the method.
2. Local variable: defined inside the method of the class.
public class User1 { //Member variables, automatic initialization public String name = "Cherry balls"; private int age = 13; String phoneNumber = "13145982266"; public void print() { System.out.println("full name:"+name); System.out.println("Age:"+age); System.out.println("cell-phone number:"+phoneNumber); //Local variables need to be initialized manually before use int age = 18; System.out.println(age); } }
(2) Scope of variable
1. Member variables:
(1) The scope of the member variable is visible within the whole class and is applicable to all methods in the class;
(2) Java will give the member variable an initial value.
2. Local variables:
(1) The scope of a local variable is limited to the method that defines it;
(2) Java does not give local variables an initial value.
3. In the same class, when the member variable and the local variable have the same name, the local variable has higher priority.
public class User1 { //Member variables, automatic initialization public String name = "Yuan Longping"; public void print() { String name = "Edison"; // Local variables with the same name as member variables System.out.println("A great name:" + name); //Local variables are preferred (output Edison) System.out.println("A great name:" + this.name); //Emphasize the use of member variables (output Yuan Longping) } }
2, Access modifier
Access restrictions in Java are determined by its access modifiers.
Access modifier: used to restrict access rights in Java; In general, you can modify methods and member variables.
1. Public: public modifier, which can be accessed anywhere
2. private: only the current class can access
3. No access modifier: it can be accessed in the same package
4. protected: the current class and subclasses can be accessed
public class User1 { public String name = "Lin Daiyu"; //Public access modifier: public global method public void print() { String name1 = "Jia Baoyu"; System.out.println(name); System.out.println(name1); } //Modify method: private access private void dosth1() { String name = "Xue Baochai"; System.out.println(this.name); System.out.println(name); } //No access modifier: in package method void dosth2() { String name = "Xue Baochai"; System.out.println(this.name); System.out.println(name); } //protected access decoration: the current class and subclasses can access protected void dosth3() { } }
3, static modifier
Static modifier: a static modifier that modifies member variables, methods, and code blocks.
1. static modifier member variable
2. static modification method
Static modified methods belong to static methods. Static methods can be called directly through classes (eliminating the tedious creation of objects and waste of resources), but static methods cannot call non static methods.
public class java{ //Static member variable private static int x; //Instance member variable private int y; //Static method public static void dosth1() { } public static void dosth2() { //Static methods allow static methods to be called dosth1(); x = 34 ; //Static methods are not allowed to call instance methods //dosth3(); //Static methods are not allowed to call instance member variables //y = 99; } //Example method public void dosth3() { //Instance methods allow static methods to be called dosth2(); dosth1(); x = 65; y = 32; }
4, Code block
The code enclosed by "{}" in Java is called code block.
1. Common code block
Method body of method in class
2. Local code block
The local code block is defined in the method, which can limit the life cycle of the variable. When the contemporary code block runs, the variable will be released, so as to save memory space.
3. Construct code blocks
In the code fragment enclosed by {}, the construction block will be called when creating the object, and will be called when creating the object, and will take precedence over the class constructor.
4. Static code block
Static code blocks are also defined in classes. Compared with constructing code blocks, only a static modifier is added before curly braces.
//Member variable String name; //Static code block // When the Service class is used for the first time (the JVM loads the class), execute the static code block once (only once) static { System.out.println("Service The static code block 1 of the class is executed"); // Calling instance methods is not allowed // dosth1(); // Allow calls to static methods dosth2(); } //Construct code block //The construction method is executed, and all construction code blocks are executed first (in the order of definition) { System.out.println("Service The construction code block 1 of the class is executed"); dosth1();//Allow calling instance methods dosth2();//Allow calls to static methods } //Parameterless construction method (no return value) public Service() { System.out.println("Service The parameterless construction method of the class is executed"); } //Parametric construction method public Service(int x) { System.out.println("Service The parameterized constructor of the class is executed"); } //Example method public void dosth1() { //Local code block { System.out.println("dosth1"); } } //Static method public static void dosth2(){ System.out.println("dosth2"); }
5, Encapsulation
1. Why to encapsulate: in a class, the attribute value is directly exposed. Anywhere you want to obtain the value, you can obtain the attribute value without any obstacles. It is unsafe in itself.
2. Encapsulation: hide the attributes and implementation details of the object, only expose the interface to the public, and control the access level of attribute reading and modification in the program.
3. Implementation steps:
(1) Modify the attribute visibility and set the access of the attribute to private decoration.
(2) Create getter/setter method, and set the method to read and write properties.
(3) Strengthen the getter/setter method and add attribute control statements to the getter/setter method.
public class User01 { //Set the access of the property to private private int age;//Age //Storage: setter method public void setAge(int age) { if(age < 18 || age > 150) { this.age = 18; }else { this.age = age; } } //Get: getter method public int getAge() { return age; } } public static void main(String[] args) { User01 user = new User01(); user.setAge(19); System.out.println(user.getAge()); }