** International cases come in first point like **Copy the code

Say packaging, let’s talk about life packaging, for example, you want to buy a mobile phone, you can only place an order on Taobao, this mobile phone how to produce, how to write the code inside, you do not need to understand, this is packaging, that is, the manufacturer directly to the mobile phone to you package, you pay to use it. In fact, encapsulated ideas can be understood as social division of labor!

1. The concept of encapsulation

Encapsulation is one of the three characteristics of object orientation. Encapsulation is an interface that hides implementation details and only provides external access. I don’t care about the implementation details inside. Just like the boss arranged a task, I don’t care how helpless and painful you are, I only want the result! Encapsulation includes: attribute encapsulation, class encapsulation, component encapsulation, modular encapsulation, system – level encapsulation

2. Benefits of encapsulation

  • Can modular, is the division of labor is more clear, what is that module very clear
  • Information hiding
  • Code reuse encapsulates, when called again, directly to call the line, do not have to write a lot of repeated code!
  • Plugins are easy to debug
  • Having security is safe for the caller. If something goes wrong, it’s not the caller’s fault, it’s their fault! Disadvantages of encapsulation: it will affect the efficiency of code execution, we are in order to improve the maintainability of the program, it is worth sacrificing a little efficiency.
/** encapsulation If attributes are not encapsulated, it is not safe to access attributes of the class directly from outside the class
import java.util.Arrays;
public class Test8{
	public static void main(String[] args){	
	Person p1=Person();
	p1.name="Fifi";
	p1.age=18; }}// No encapsulation before
class Person{
	// Define attributes in a class
	String name;
	int age;
}

Copy the code

So there’s no problem compiling and running, so let’s encapsulate, encapsulate the code

Private keyword private public Public Private attributes and methods can only be used in this class. Common attributes and methods can be accessed directly from other classes
import java.util.Arrays;
public class Test8{
	public static void main(String[] args){	
	Person p1=Person();
	p1.name="Fifi";
	p1.age=18; }}// No encapsulation before
class Person{
	// Define attributes in a class
	private String name;
	private int age;
}
Copy the code

As you can see, just add private in front of the property, and then we’ll compile the code above


/** encapsulation If the attribute is not encapsulated, then the class attribute can be accessed directly from outside the class, which is not safe private keyword private */
import java.util.Arrays;
public class Test8{
	public static void main(String[] args){	
	Person p1=Person();
	/ / p1. Name = "Fifi";
	//p1.age=18;
	p1.setName("Fifi");
	p1.setAge(18); }}// No encapsulation before
class Person{
	// Define attributes in a class
	private String name;
	private int age;
	// All getSet methods are written like this
	public void setName(String name){
		this.name=name;// This is the name of the method parameter
		//this represents the current object
	}
		// Provide a method to get the name attribute externally
	public String getName(a){
		return name;
	}
	// For the same reason, we write the age certificate of entry to Beijing
	
	public void setAge(int age){
		this.age=age;
	
	}
		// Provide a method to get the name attribute externally
	public int getAge(a){
		returnage; }}Copy the code

Some children shoes will ask, packaging and packaging before the effect is not the same? All set attributes, what’s the difference? If we write it this way, we get the same result, but the meaning is different, the meaning before the package is that I stole 100 dollars from you, the meaning after the package is that I borrowed 100 dollars from you, are these two meanings the same? Name =”shabi”; name=”shabi”; name=”shabi”; I set the age. What if I set it to negative? What if I set it to set? For another example, is it possible for all cars to get a Beijing permit? What if it’s a black car? We can do this in code!

// No encapsulation before
class Person{
	// Define attributes in a class
	private String name;
	private int age;
	// All getSet methods are written like this
	public void setName(String name){
	    if("shabi".equals(name)){
			return;
		}
		this.name=name;// This is the name of the method parameter
	
	}
		// Provide a method to get the name attribute externally
	public String getName(a){
		return name;
	}
	// For the same reason, we write the age certificate of entry to Beijing
	public void setAge(int age){
        if(age>150||age<0) {return;
		}
		this.age=age;
	}
		// Provide a method to get the name attribute externally
	public int getAge(a){
		returnage; }}Copy the code

Usually in a class, properties are privatized and provide getter and setter methods. This is the encapsulation of properties!

Member variables and local variables

  • Different position in a class Member variables: defined in a class Local variables: defined in a method or method parameters
  • Member variables: in heap memory (member variables belong to objects, objects go into heap memory) Local variables: in stack memory (local variables belong to methods, methods go into stack memory)
  • Different life cycles Member variables: Exist with object creation and disappear with object destruction Local variables: exist with method invocation but disappear with method completion
  • Local variables have no default initialization value. They must be defined, assigned, and then used
Note: Local variable names can be the same as member variable names; when used in methods, the nearest rule applies.Copy the code

Example code:

public class Test8{
	public static void main(String[] args){	
	Person p1=Person();
	/ / p1. Name = "Fifi";
	//p1.age=18;
	p1.setName("Fifi");
	p1.setAge(18); p1.run(); }}// No encapsulation before
class Person{
	// Define attributes in a class
	private String name;// Member variables defined in the class
	private int age;/ /; // Member variables defined in the class
	public  void run(int leng){// Parameters are also local variables
		int m=length;M is also a local variable}}Copy the code