This section of the Knowledge Points:
- Self in a class method
- Self in the object method
- Global variable member variable local variable
- Self summary
- Self Use note
- OC mention?? Two reserved words for self and super
- The self in the OC language is equivalent to the this pointer in C + + and Java.
- To understand what self is, what is a member variable, what is an object method, what is a class method
- Member variables: Member variables are specific state characteristics of an instance object, and these state characteristics can be changed, such as the age of Zhang San, height, weight, etc.
- Object methods: The behavior of an instance object, such as Zhang San has eating behavior, Zhang San to make such behavior, it is possible to affect, some of their own state characteristics, such as Zhang San eat may increase the weight and height of three.
- Class method: A class method is the behavior of a class that can be called directly from a class name, and if some data needs to be used in a class method, it must pass through a parameter; it cannot access member variables.
1. Self in the class method
- During the whole process of running the program
一个类有且仅有一个类对象 .
- By
类名 invoking the method is to 类对象 send a message to this.
- The self of the class method is the class object
- Other class methods can be called through self in a class method
- You cannot call an object method or member variable in a class method, because both the object method and the member variable belong to the specific instance object.
2. Self in the object method
- During the entire program run,
对象可以有0个或多个
- By
对象 invoking the method is to 对象 send a message to this
- In the object method, self is the current object that invokes this method.
- In an object method, you can call other methods on this object through self
- In an object method, you can access member variables through self
3. Global variable member variable local variables
- Global variable: Can be used wherever it is declared
- Member variables: can only be used in object methods of this class and its subclasses
- Local variables: can only be used in this function or method
- From scopes: Global variables > member variables > Local variables
- When a variable with the same name appears in a different scope, the variable in the inner scope overrides the outer scope variable, so the variable with the same name has the override order: local variable overrides the member variable, and the member variable overrides the global variable
- If a local variable with the same name as the member variable appears in the object method, if you want to use the member variable at this time, you can pass the self-> member variable name
4.self Summary
- Who calls the method where self is, then self is who
- Self in the class method, is the class object of this class, there is only one globally, can call the other class method in this class through self, but cannot call object method through self or Access member variable
Self in the object method, which is the object that calls this method, can access the member variable by calling other object methods in this class through self, but cannot invoke the class method of this class through self.
The format of the method is called through self: [the Self method name];
- :self-> member variable names through the Self Access member variable format
5.self Use note
- Self is not wrong when there are object methods and class methods present.
- Self can only be used in methods; do not use self to invoke a function, nor can self be used inside a function;
- This method is called with self, resulting in a dead loop call.
11:self keywords