Java analysis of one of the three major features encapsulation

Before talking about the three features of Java, let's first understand what is object-oriented and why Java is an object-oriented language.
Object-oriented is a programming idea that is different from process-oriented. We can understand the difference between object-oriented and process-oriented through the example of an elephant in a refrigerator .
Presumably through this example, the difference between object-oriented and process-oriented should be clear. Process-oriented means that we mainly focus on each stage of the process, while object-oriented means we mainly focus on the executor of each action. Java is a language that focuses on the executor of actions, on writing classes, and on code and function reuse.

package

We have mentioned above that Java focuses on the writing of classes, so encapsulation is naturally the encapsulation of classes. There are many benefits to class encapsulation.

  1. The internal implementation of the method can be completely hidden, and only a method to call is provided to others, so that other people who use this class do not need to care about how it is implemented, as long as they know how to call it.
  2. The advantage of hiding the internal implementation of the method allows you to modify the structure of the class at will while keeping the calling method unchanged, without affecting the results of other people's operations.
  3. Encapsulation also separates the properties of the class into private and public properties. Private properties can only be invoked by the class itself, and public properties can only provide a method for external invocation.
  4. In software terms, good encapsulation reduces coupling.

How to encapsulate a class needs to be based on the objective attributes and actual needs of the class itself.
For example a MyTime class

public class MyTime{
    public String date;
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式 date= df.format(new Date()); public String getDate() { return date; } // // public void setDate(String date) { // this.date = date; // } }

Obviously, this class only provides the system time display, and does not provide an external method to modify the system time. This is also based on the objective properties of the class itself, time is an inherent property that cannot be changed artificially, of course, there are many such examples. We are just a simple example to show how we should design the external access method of the class.

access permission

In the above code, we have seen that we can call the getData() method to access date, but the modifier of date is public. In other words, we can completely modify and read the date property by creating a new MyTime object. It goes against the original intention of our packaging. So, Java introduced different access rights to limit this thing. In "Java Programming Ideas", the following access rights
public: interface access rights are explained. That is to say, public modified member methods and properties can be accessed anywhere.
private: you cannot access. That is to say, private modified member methods and attributes can only be accessed in this class. Outside this class, you cannot access the content of this class. This is very useful. Our example code above , because it is public modification, there will be a problem of incomplete encapsulation. If our member variable date is modified with private, then the above problem will not occur.
protected: Inherited access rights. Before talking about inheriting access rights, let's think about the problem that we have a base class that needs to be inherited. We hope that the member properties in the base class can be accessed by the inherited class, then we can set it as public, but once this is the case, other classes will also access it except the inherited class. This is obviously not what we expect, so Java introduces the protected modifier to indicate that the modified part can be accessed by derived classes, and it can also be accessed by other classes in the same package.
So, to sum up, if we want the methods and properties in the class to be accessible by all classes, use public, if it can only be accessed by inherited classes and classes in the same package, use protected, if only want to be in the current If it is accessed in a class and cannot be accessed by any other class, use private.

Summarize

After understanding the specific meaning and benefits of encapsulation, we can use the above access rights to encapsulate what we want to encapsulate, hide the specific implementation through the access rights, and provide an interface for external access.

public class Student {
    private String name;
    private String age; private String handleName(String name){ return "I'm " + name; } private String handleAge(String age) { return age + " 岁"; } public String getName() { return name; } public void setName(String name) { this.name = handleName(name); } public String getAge() { return age; } public void setAge(String age) { this.age = handleAge(age); } }

In the above example, we simply encapsulate the Student class. First, we cut off the possibility of calling name and age directly from the outside, and provide access to name and age through the set and get methods. At the same time, the above example It is also reflected in that, when the external access method remains unchanged, we can modify the internal implementation at will. We first wrote the Handle method that is only used for internal calls of the class, and used the Handle method to modify the display results of name and age. This ensures that the method of modifying the displayed result can only be called by itself, and the internal implementation can be modified according to the requirements under the condition that the external access remains unchanged. This is also a simple encapsulation example. An example of its call is as follows: the student object can only call the following content, and other content written by ourselves cannot be called.

public class Test {
    public static void main(String[] args) { Student student = new Student(); student.setName("byhieg"); student.setAge("23"); System.out.println(student.getName() + " " + student.getAge()); } }

Guess you like

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