Java Summary (i): Package--encapsulation

Source: Internet
Author: User

Official definition: A method of wrapping and hiding the actual details of an abstract functional interface. Encapsulation can be considered a protective barrier against the code and number of this class
Randomly accessed by code defined by the external class.

Plain English definition: access to private-member variables through getter and setter methods.

Function:
1. It's easier to modify your own implementation code without modifying the program fragments that call our code.
eg. Change the following age types from int to String.

1 classhusband{2    Public intAge ;3    PublicString name;4 }5 6 classwife{7   Private intAge ; 8   PrivateString name;9 Ten   voidSetage (intAge ) { One      This. Age =Age ; A   } -  -   voidsetName (String name) { the      This. Name =name; -   } -  -   intGetage () { +     returnAge ; -   } +  A String GetName () { at     returnname; -   } -  - } -  -  in  Public classtestencapsulation { -      Public Static voidMain (string[] args) { toHusband H1 =NewHusband (); +H1.age = 21; -H1.name = "A1"; the  *Husband H2 =NewHusband (); $H2.age = 22;Panax NotoginsengH2.name = "A2"; -              the              +Wife W1 =NewWife (); AW1.setage (18); the w1.getage (); +              -Wife W2 =NewWife (); $W2.setage (19); $ w2.getage (); -              -              the              - System.out.println (h1.age);Wuyi System.out.println (W2.getage ()); the  -    } Wu}

Change: Husband class to change peripheral calling program, Wife class need to change. At the same time, assuming that there are 100 husband objects and 100 wife objects, it is easy to change the wife class.

1classhusband{2 Public StringAge//change to String3 PublicString name;4 } 5 6classwife{7Private StringAge//change to String8PrivateString name;9 10voidSetage (intAge ) {11 This. Age= string.valueof (age);12   }13 14voidsetName (String name) {15 This. Name =name;16   }17 18StringGetage () {//change to String19returnAge ;20   }21 22String GetName () {23returnname;24   }25 26 }27 28 29 Public classtestencapsulation {30 Public Static voidMain (string[] args) {Husband H1 =NewHusband ();32h1.age = "+";  //Change the calling program   H1.name = "A1";Husband H2 =NewHusband ();36h2.age = "a";PNS h2.name = "A2";Wife W1 =NewWife ();W1.setage (18);42w1.getage ();Wife W2 =NewWife ();W2.setage (19);46w2.getage ();47 48 49 50System.out.println (h1.age);51System.out.println (W2.getage ());52 53    }54}

Java Summary (i): Package--encapsulation

Related Article

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.