I. Object methods and class methods
Object method:
1) start '-'
2) Access instance variables
3) class methods can be called.
4) You can call methods of yourself and other objects (call methods of other objects and pass them as parameters)
5) called by an object
Class method:
1) '+'
2) instance variables cannot be accessed.
The instance variables cannot be accessed in a class method, because the instance variables have not been allocated memory, initialized, or can be said that there are no instance variables, because there are no objects
3) class methods can call other class methods
4) class methods cannot be called using objects.
5) class method, which can call the object method (passing the object as a method parameter)
6) The current method (endless loop) cannot be called in class methods)
For example, the person class calls its own class method reach
+ (Void) reach;
{
[Person reach];
}
[Person reach]; // Infinite Loop
Note: Object methods and class methods can have the same name.
Ii. Use of anonymous classes
1> first usage of the anonymous class:
Person * P = [person new];
// Create a vehicle object
// Display object used
Car * car1 = [Car new];
Car1-> _ color = 1;
// People drive home
[P Gohome: [Car new]; // anonymous usage
The object passed in by the person object method Gohome is an anonymous object [Car new];
2> second usage of the anonymous class
Anonymous classes can call Methods
[[Car new] Run];
3> second usage of the anonymous class
The member variables of the category class do not actually achieve what I want.
// [Car new] A new storage space will be allocated for each execution.
[Car new]-> _ color = 2;
Iii. Scope of member variables
Iv. Encapsulation
Class encapsulation:
Only the object methods of the class can be used to define the member variables of the class.
The set and get methods are provided for the member variables of the class.
Set sets the member variables of the class. Set Method
@ Interface car: nsobject
{
// Instance variable
Int _ lunzi;
Int _ color;
}
// Define the Set Method
-(Void) setlunzi :( INT) lunzi;
// Define the get Method
-(INT) lunzi;
// Set color
-(Void) setcolor :( INT) color;
-(INT) color;
@ End
@ Implementation car
// Define the Set Method
-(Void) setlunzi :( INT) lunzi {
_ Lunzi = lunzi;
}
// Define the get Method
-(INT) lunzi {
Return _ lunzi;
}
// Set color
-(Void) setcolor :( INT) color {
_ Color = color;
}
-(INT) color {
Return _ color;
}
@ End
Writing specifications:
Set Method:
1) object Method
2) set the value of the member variable. The set method can return no value.
3) Name of the member variable with the set + underline removed, and the first letter of the member variable must be capitalized.
4) since you want to set the value of the member variable, you have to pass the value to be set.
The passed value type must be consistent with the name of the member variable to be set.
The parameter is generally a member variable name that removes the underline.
Get gets the value of the member variable of the current class. The get method can be used to perform lazy loading: loading as needed)
Get Method
1) object Method
2) to obtain the value of a member variable, the method must return a value.
3) The returned value type must be consistent with the type of the member variable to be returned.
4) The method name is generally a member variable name that removes the underline.
Advantages of encapsulation: 1. The value of Object Attributes cannot be modified externally, but can only be modified using the object method.
2. You can reasonably judge the rationality of the externally passed values.
V. Combination
Encapsulation of the first thought class of OC