Java attribute Encapsulation

Source: Internet
Author: User

//Encapsulation of//Package Concept://1, is one of the three characteristics of object-oriented thinking//2, the package details are hidden, only provide access to the external interface//3. Package Classification://attribute Encapsulation//Method Encapsulation//Encapsulation of Classes//Encapsulation of Components//Modular Package//system-Level encapsulation//Package Benefits:/**Modular information Hiding code reuse plug-in easy to call with security*///The simplest attributes are encapsulated: Public classtest17{ Public Static voidMain (String args[]) {Dog D1=NewDog (); //d1.name= "Xiaodeng"; //d1.age=28;D1.setname ("Xiaodeng");    D1.print (); }}classdog{PrivateString name;//private and cannot be accessed directly from outside    Private intAge ; //How do I get people to access from outside? --Encapsulation//Getter and Setter     PublicString GetName () {returnname; }         Public voidsetName (String name) { This. name=name; }    voidprint () {System.out.println ("Name:" +name+ "\nage:" +Age ); }}

Java attribute Encapsulation

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.