1, Basic introduction
Encapsulation is a class of some fields, methods to protect, not to be accessed by the outside world, there is a control function of permissions, Java has four kinds of access modifiers:
Public,default,protected,private
The access rights are decremented in turn, so that when we define the class, which fields and methods do not want to be exposed, which fields and methods can be exposed, can be done by modifiers, this is the encapsulation, let's look at an example:
#import @interfacecar:nsobject{//This property is the equivalent of private, so we need to define the Get/set method for external access.//The default is private, but we can use @public to set the public property, then the external can be directly accessed: person->capcity = 2.8; //Of course we don't use this as much, because it destroys encapsulation, which is equivalent to the permissions in structs in C//There are four kinds: @public, @protected, @private, @package, and this is the same as in Java.@public float_capcity;//Oil Quantity Properties} - (void) Run: (float) T; @end
As we can see here, there are also four types of access modifiers in OC:
@public, @protected, @private, @package
Where the default modifier is @private
However, it is important to note that the method in OC is not the concept of modifiers, this and Java has a great difference, is generally publicly accessible, that is, public, but how do we make a method in OC can not be accessed by the outside world?
This is done in OC, if you want a way to not be accessed by the outside world, Only need to implement this method in the. m file, do not define in the header file, it is: The method is implemented, not defined, so that the outside world in the import of the header file, there is no such method, but this method can be used in their own. m file
2. Set and Get methods1. Use of Set method and get method
@public members can be arbitrarily assigned, you should use the Set method and the Get method to manage the access of members (like airport security, tap filtering, filter out unreasonable things), such as zombie health value can not be negative
2. Set method
1) Function: Used to set the member variable, you can filter out some unreasonable values in the method
2) Naming specification:
Method starts with a set, followed by the member variable name, and the first letter of the member variable name must be capitalized
Parameter names do not have the same name as member variables
3. Get method
1) Function: Returns the member variable inside the object
2) Naming specification: The name of the Get method is typically the same as the member variable
4. Naming conventions for member variables
Member variables begin with the underscore _
Can be separated from the name of the Get method
Can be separated from other local variables, a variable that starts with an underscore, is definitely a member variable
3. code example
1 #import<Foundation/Foundation.h>2 //Statement3 @interfaceCar:nsobject4 {5 int_wheels;//Number of wheels6 }7 /*Set Method*/8- (void) Setwheels: (int) Wheels;9 /*Get Method*/Ten- (int) Wheels; One @end A - @implementationCar - //implementation of Set method the- (void) Setwheels: (int) Wheels - { - //filter the number of wheels that come in from outside - if(wheels<=0) + { -Wheels =1; + } A at_wheels =Wheels; - } - - //implementation of the Get method -- (int) Wheels - { in return_wheels; - } to @end
The three characteristics of OC--encapsulation