//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