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