Encapsulation of java--data domains

Source: Internet
Author: User

Encapsulated instances of data fields:

1 classCirclecount2 {3     Private Doubler=1.0;4     Private Static intCount=0;5 Circlecount ()6     {7circlecount.count++;8     }9Circlecount (DoubleR)Ten     { One          This. r=R; Acirclecount.count++; -     } -      Public DoubleGetr () the     { -         return  This. R; -     } -      Public voidSETR (DoubleR) +     { -          This. r= (r>0?r:0); +     } A      Public Static intGetCount () at     { -         returncount; -     } -          Public DoubleGetarea () -     { -         returnr*r*Math.PI; in     } - } to  Public classTest + { -      Public Static voidmain (String [] args) the     { *Circlecount c1=NewCirclecount (); $C1.SETR (8);Panax NotoginsengSystem.out.println ("c1.r=" +c1.getr () + ", c1.count=" +C1.getcount ()); -          theCirclecount c2=NewCirclecount (5); +C2.SETR (-9); ASystem.out.println ("r=" +c2.getr () + ", count=" +C2.getcount ()); the          +          -     } $}

Note: THIS.R can also be changed to r, no effect. where R and Count are encapsulated.

1. Private data domains can only be accessed in the class in which they are defined. Can no longer be used in client programs. R and Count are private and cannot be modified.

2. If the customer needs storage, modify the data field. You can use the Get method (GETR) to return the value of the data and set the new value with the Set method (SETR).

Encapsulation of java--data domains

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.