Encapsulation process:
(Recommended learning: java introductory program)
First, the variables will be privatized through private. Restrict access to class attributes
Then set the external access interface for each attribute through the set and get methods. Externally, if you need to change the attributes of the class, you need to use these public methods.
Then you need to create an instance of the encapsulated class, such as creating a Test class, making the variables private and the methods public. The set and get methods can be automatically generated
(Video tutorial recommendation: java video tutorial)
Continue to create a Test2 class, create an object of the Test class, and set the properties of the encapsulated class through the object , and then obtain these properties through the get method
Finally run the project, and you can see on the console that the properties of the encapsulated class have been successfully obtained
The above is the detailed content of How to achieve encapsulation?. For more information, please follow other related articles on the PHP Chinese website!