1. Note Points for encapsulation
1: //Member variable try not to use @public
2: //@public
3: int age;
1: //@public
2: //Read Only (readonly): Only allow outside access to my no, not allow the outside world to modify my no
3: int No;//Only a Get method is required
2.get, set method
1: Set method
2: 1. Function: Provide a method to set the value of member variables to the outside world, can be in the method to face the parameters of the corresponding filter
3: 2. Naming specification:
4: 1> method name must start with set
5: 2> set followed by the name of the member variable, the first letter of the member variable must be capitalized
6: 3> return value must be void
7: 4> must receive a parameter, and the parameter type is consistent with the member variable type
8: The name of the 5> parameter cannot be the same as the member variable name
1: Get method
2: 1. Function: Returns the member variable inside the object
3: 2. Naming specification:
4: 1> must have a return value, the return value type must be the same as the member variable type
5: 2> method name is the same as member variable name
6: 3> does not need to receive any parameters
1:
2: //implementation of the Set method
3: -(void) Setage: (int) newage
4: {
5: //filter The parameters passed in
6: if (newage <= 0)
7: {
8: newage = 1;
9: }
10:
One : Age = newage;
: }
:
: -(int) age
: {
: return age;
: }
3. Naming conventions for member variables:
Be sure to start with the following dash _
Role:
1. To separate the name of the member variable from the Get method
2. Can be separated from the local variables, a see the beginning of the underscore variable, is usually a member variable
Example
1: //The set and get methods for sex
2: -(void) Setsex: (Sex) sex;
3: -(sex) sex;
4:
5: //Set and get method for no
6: -(void) Setno: (int) No;
4. Practical examples of encapsulation
1: /*
2: 4. Design a score class
3: * C language score (readable and writable)
4: * OC result (readable and writable)
5: * Total score (read only)
6: * Average score (read only)
7: * /
8: #import <Foundation/Foundation.h>
9:
: @Interface Score:nsobject
One : {
Int. : int//C language score
Int. : int//OC score
14:
: int _totalscore; Total
: int//Average score
: }
:
: -(void) Setcscore: (int) Cscore;
: -(int) Cscore;
£ º
: -(void) Setocscore: (int) Ocscore;
At : -(int) Ocscore;
:
: -(int) Totalscore;
: -(int) Averagescore;
:
: @end
£ º
: @implementation Score
: -(void) Setcscore: (int) Cscore
: {
: _cscore = Cscore;
34:
: //Calculate total score
£ º _totalscore = _cscore + _ocscore;
Panax notoginseng : _averagescoe = _TOTALSCORE/2;
: }
The following :-(int) Cscore
Max : {
A : return _cscore;
: }
:
: -(void) Setocscore: (int) Ocscore
: {
: _ocscore = Ocscore;
47:
Sum : //Calculate total score
£ _totalscore = _cscore + _ocscore;
: _averagescoe = _TOTALSCORE/2;
Wuyi: }
: //change of listener member variable
:
Wu : -(int) Ocscore
: {
£ º return _ocscore;
£ º }
:
Totalscore : -(int)
: {
: return _totalscore;
+ : }
Averagescore : -(int)
: {
: return _averagescoe;
: }
: @end
:
:
: int main ()
: {
: new];
73:
A : [s setcscore:90];
: [s setocscore:100];
76:
: [s setcscore:80];
78:
79:
n : int a = [s totalscore];
81:
: NSLog (@ "total:%d", a);
83:
+ : return 0;
: }
Black Horse programmer--"Dark Horse video note" OC Language Base code encapsulation