Home  >  Article  >  Java  >  What is the concept of Java encapsulation

What is the concept of Java encapsulation

WBOY
WBOYforward
2023-05-09 08:52:221731browse

Encapsulation

The concept of encapsulation

hides certain information of a class inside the class and does not allow direct access by external programs. Instead, it uses the methods provided by the class to access the hidden information. information to operate and access.

Why is encapsulation needed?

After we create an object of a class, we can assign values ​​to the properties of the object through "object.properties". The assignment operation here is restricted by the data type and storage range of the attribute. Apart from this, there are no other constraints. However, in practical problems, we often need to add additional constraints to attribute assignment. This condition cannot be reflected in the attribute declaration. We can only add restrictive conditions through methods. At the same time, we need to prevent users from using the "object.property" method to assign values ​​to properties, so we need to declare the properties as private.

Advantages of encapsulation:

1. Good encapsulation can reduce coupling.

2. The structure inside the class can be modified freely.

3. Member variables can be controlled more precisely.

4. Hide information and implement details.

Encapsulation implementation steps

(1) Modify the attribute to private

(2) Create a getter/setter method, and use these two methods to obtain and set data It is determined that the object can read and write data by calling these two methods.

class person{
  private int age;//修改属性为private
public int getAge(){
  return age;
}
public void setAge(int a){
   age = a;
   }
 }

Encapsulation hides the internal complexity of the object and only exposes simple interfaces to facilitate external calls. The embodiment of encapsulation requires permission modifiers to match.

Packages in Java

Packages in Java are to better manage classes in the project and resolve conflicts with files with the same name. When it comes to packages in java, we have to mention them. When it comes to the package keyword, we generally use package to declare the package to which a class or interface belongs. The declaration is in the first line of the source file, such as package java.lang. Each . represents a layer of file directories. Interfaces with the same name cannot be named under the same package. , classes, different packages can be used. For the use of packages, you need to use the import keyword. Use the import structure shown in the source file to import the classes and interfaces under the specified package. Usually the import statement is between the package declaration and the class declaration. , in java, the package name specification is spelled in all lowercase letters

Members of classes in java-constructors

Any class has a constructor, which is used to create and initialize objects. Attributes, methods to create objects of a class: new constructor, such as Person p = new Person(); If there is no explicit constructor defined for the class, the system defaults to a constructor with empty parameters.

Define the format of the constructor

Permission modifier class name (formal parameter list){}

Summary: The order of attribute assignment:

1 Default initialization

2 Explicit initialization

3 Initialization in the constructor

4 Through the method of "Object.Method" or "Object.Property" , Assignment

This keyword in java

This keyword can be used to modify: properties, method constructors, this is understood as: the current object or the object currently being created

In the method of the class, we can use "this.property" or "this.method" to call the current object properties or methods. But usually, we choose to omit "this." In special cases, if the formal parameter of the method has the same name as the attribute of the class, we must explicitly use "this. variable" to indicate that the variable is an attribute.

The same is true for the constructor of a class. Call a constructor with empty parameters: this(); if you call a constructor with parameters such as public Person (int age), you can use this(age) to make the call, but the constructor A constructor cannot call itself and can only call other constructors. If there are n constructors, then only n-1 constructors can be called using this method, and the constructor this call must be declared at the beginning of the constructor. OK, this can also be used to compare sizes between objects.

public class Boy {
    private int age;
    public int getAge() {
        return age;
    }
    public void setAge(int a) {
        age = a;
    }
    public void compare(Boy boy) {
        if (this.age > boy.age) {
            System.out.println("YES");
        } else if (this.age < boy.age) {
            System.out.println("No");
        }
    }
}

The above is the detailed content of What is the concept of Java encapsulation. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete