Java basics-encapsulation and java Encapsulation
I recently learned about Java object-oriented knowledge, but I haven't had time to update my blog, because this knowledge point is really around. It takes me a whole day to differentiate constructors and methods. Now I want to repeat the knowledge point again.
First, we need to consider the following idea:
Object-Oriented Programming puts data first and then considers data operations.
Before learning about the advantages of Java, we learned that Java is an object-oriented programming language. So what is object-oriented programming? To understand it, first let's take a look at another program design.
I. process-oriented programming:
Process-oriented programming is implemented sequentially from top to bottom and gradually refined; its program structure is divided into several basic sections by function, which form a tree structure; the relationship between each module is as simple as possible, and its functions are relatively independent. Each module is composed of three basic structures: sequence, selection, and cycle; the specific implementation method of the board is the use and subroutine. The program process is determined when writing the program.
(This concept was found on the Internet when I was learning object-oriented programming. I copied it and the concept of object-oriented programming on a blank sheet of paper, in fact, I don't know what process-oriented programming is. But when I finish learning the object-oriented and class, I can come back and look at this concept. I did not know the process orientation and did not dare to speak too much. I just learned the object orientation and did not dare to comment more. According to my understanding, the process orientation is like making all the workflows of a thing interlocking. Object-oriented, It is very intelligent. It is like a manufacturer that manufactures large machinery. It doesn't always make its own parts, but it can be assembled by all means (of course, you also need to follow the rules set by others.) The advantage is that you don't have to worry about the project being affected, especially on the client .)
Ii. object-oriented programming:
Object-Oriented Programming (OOP) is a mainstream programming example. It has replaced the "structured" procedural programming development technology in the 20th century. Object-oriented means to put data and data operation methods together. As an interdependent whole, we call it an object. Abstract The commonalities of similar objects to form a class. Class. A simple external interface is used to communicate with an object through a message. The procedure is determined by the user's usage. Java is fully object-oriented. You must be familiar with OOP before you can write Java programs.
(Figure) demonstrate differences between object-oriented programming and process-oriented programming
The following sentence is very important:
An Object-Oriented Program is composed of objects. Each object contains specific functions and hidden implementations exposed to users. (This sentence is really too important. I will be able to understand it in the future when I study Singleton, encapsulation, abstract classes, and interfaces !)
Iii. Category
A class is a template or blueprint for constructing objects. The construct object process becomes the instance for creating the class ).
When I am learning this, I will be a little scared. So many classes need to be remembered? Do you want to create a class yourself? Can the content in the self-created class be comprehensive? All kinds of questions are lingering. Actually, all the code written in Java is mostly inside a class. The standard Java Library provides thousands of classes for user interface design, date, calendar, and network programming. However, we also need to add, delete, and modify our own projects based on the actual situation.
Iv. Encapsulation
Encapsulation (encapsulation, sometimes known as data hiding). When watching video classes, they describe encapsulation to get and set methods. In terms of form, encapsulation only combines data and behavior in a package, and hides the data Implementation Method for the object user.
The data in the object is called the instance field, and the process of operating the data is called the method ). Each specific class instance (object) has a set of specific instance domain values. The set of these values is the current state of the object ).
The following sentence is important:
The key to encapsulation is that the methods in the class cannot directly access the instance domains of other classes. The program only interacts with the object data through the object method.
Encapsulation gives the object a Black Box feature, which is the key to improving reusability and reliability. This means that a class can completely change the way data is stored. As long as the same method is used to operate data, other objects will not know or care about the changes.
// What does this sentence mean? That is to say, if you want to define a Java class someday, you don't have to worry about other classes being disturbed by you. You can expand a class to create a new class you need. Don't forget, in fact, all classes in Java are from an Object. In the future, I will continue to introduce the Object.
==== Encapsulation process:
1. encapsulation is to privatize attributes (private );
2. You need to provide the get/set Method (not all get/set methods are proposed, depending on your own needs. If you do not want others to get it, you will not provide get; if you do not want others to set it, you will refuse to provide set .)
=== ACL:
| Accessors |
In this class |
In this package |
This steamed stuffed bun class |
Outsourcing |
Outsourcing subclass |
| Public (common) |
Accessible |
Accessible |
Accessible |
Accessible |
Accessible |
| Protected |
Accessible |
Accessible |
Accessible |
Inaccessible |
Accessible |
| Default |
Accessible |
Accessible |
Accessible |
Inaccessible |
Inaccessible |
| Private) |
Accessible |
Inaccessible |
Inaccessible |
Inaccessible |
Inaccessible |
Sample Code:
1 public class XiaoNanHai {2 private int age; 3 public void setAge (int age) {4 if (age >=15 & age <= 35) 5 this. age = age;} else {6 System. err. println "the age you entered is not in the specified range. Please enter it again"); 7} 8}