Encapsulation method or Property

Source: Internet
Author: User

Encyclopedia: Encapsulation: Refers to the properties of hidden objects and implementation details, only provide public access to external, such as computer chassis, notebook, etc.
Benefits:
Isolate the changes;
easy to use;
Improve reusability;
Improved security (prevent arbitrary modification)
One package, can be called indefinitely, and the attribute can be qualified
Keyword private: The embodiment of encapsulation in code
(1) private meaning, permission modifier
(2) To modify member variables and member functions
(3) Members with private decoration are valid only in this class, and members of this class can call the private property directly
(4) Private is a manifestation of encapsulation
The advantage of encapsulating a basic data type as an object is that more functionality can be defined in the object to manipulate the data.

Personal: Set method has parameters, no return value, public void setage (int age) No return
The Get method has no parameters, there is a return value of public int getage () return of age;

1 //For example, the following code:2  3 Private intAge ;4  Public voidSetage (intAge ) {5 6 if(age>150| | Age<0){7 return;//when age is assigned to a value that does not meet the requirements, returns an empty8 }9  This. age=age;//when it meets the requirementsTen } One  Public intGetage () { A returnAge ; -  -}

Encapsulation method or Property

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.