IOS Development Learning SUMMARY objective-c object-oriented-class and object (bottom)
Knowledge Point Security:If the access permission permits, objective-c allows access to member variables directly through objects. Syntax format:Object> member variable name;
Object and pointer
Here we will use the previous objective-c object-oriented code-class and object (I.
In FKPersonTest. m, there is such code:
FKPerson* person = [[FKPerson alloc] init];
This line of code generates two things: one is the person variable and the other is the FKPerson object. This FKPerson object is assigned to the person variable.
From the definition of the class, the FKPerson object contains three member variables (two exposures and one hidden), and the member variables need to be stored in memory. Therefore, when creating a FKPerson object, you must have the corresponding memory to store the member variables of the FKPerson object. Storage of FKPerson objects in memory:
As shown in the figure, the FKPerson object consists of multiple memory blocks. Different memory blocks store different member variables. Assign the FKPerson objectFKPerson*
The first address of the FKPerson object in the memory is actually assignedFKPerson*
Variable. FKPerson * type variables point to the actual object.
Essentially, a class is a pointer type variable. Therefore, the program-defined FKPerson * type only stores an address value, which is saved in the dynamic storage area of the main () function. It points to the actual FKPerson object, the real FKPerson object is stored in heap memory. As follows:
After an object is successfully created, it will be stored in the heap memory. objective-c does not allow direct access to objects in the heap memory and can only access the object through the pointer variable of the object. That is to say, all objects can only access them through pointer variables. Objects in heap memory can have multiple pointers, that is, multiple pointer variables can point to the same variable.
If no variable points to the object in the heap memory, the program will no longer be able to access the object. If the programmer does not release the memory occupied by the object, it will cause memory leakage.
Self keyword
The self keyword always points to the object that calls this method.Its biggest role is to allow one method in the class to access another method or member variable of the class.
Self always represents the object of the current class. When self appears in a method body, it represents an object that is uncertain. However, its type is determined: It indicates that the object can only be an example of the current class. When this method is called, the object it represents can be determined: Who calls this method, self represents who.
Sample Code:
Header file: FKDog. h
# Import
@ Interface FKDog: NSObject // defines a jump Method-(void) jump; // defines a run method. The run method must use the jump Method-(void) run; @ end
Implementation file: FKDog. m
# Import FKDog. h @ implementation FKDog // implement a jump Method-(void) jump {NSLog (@ is executing the jump method);} // implement a run method, run {// FKDog * d = [[FKDog alloc] init]; // [d jump]; [self jump]; NSLog (@ running the run method);} @ end
FKDogTest. m file:
# Import
# Import FKDog. hint main (int argc, char * argv []) {@ autoreleasepool {// create the Dog object FKDog * dog = [[FKDog alloc] init]; // call the run method of the Dog object [dog run];}
Distinguish between local variables and member variables with duplicate names using self
When the names of local variables and member variables are the same, the local variables hide the member variables. To strongly reference member variables in a method, you can use the self keyword to differentiate them.
Sample Code:
FKWolf. h file
# Import
@ Interface FKWolf: NSObject {NSString * _ name; int _ age;} // defines a setName: ageAge method-(void) setName: (NSString *) _ name andAge: (int) _ age; // defines an info method-(void) info; @ end
FKWolf. m file
# Import FKWolf. h @ implementation FKWolf // defines a setName: ageAge method-(void) setName: (NSString *) _ name andAge: (int) _ age {// when a local variable hides a member variable, // you can use self to indicate the object that calls this method. In this way, you can assign values to the member variables that call this method. Self-> _ name = _ name; self-> _ age = _ age;} // define an info method-(void) info {NSLog (@ My name is % @, age is % d, _ name, _ age) ;}@ endint main (int argc, char * argv []) {@ autoreleasepool {FKWolf * w = [[FKWolf alloc] init]; [w setName: @ andandage: 8]; [w info];}
Use self as the return value of a common method
When self is used as the default reference of an object, the program can access this self reference just like accessing a common pointer variable, or even regard self as the return value of a common method.
Example program:
ReturnSel. m file
# Import
@ Interface ReturnSelf: NSObject {@ public int _ age;}-(ReturnSelf *) grow; @ end @ implementation ReturnSelf-(ReturnSelf *) grow {_ age ++; // return self, return the object that calls this method return self;} @ endint main (int argc, char * argv []) {@ autoreleasepool {ReturnSelf * rt = [[ReturnSelf alloc] init]; // you can call the same method consecutively [[[rt grow]; NSLog (the value of @ rt's _ age member variable is: % d, rt-> _ age );}}
Note: Using self as the return value of the method can make the code more concise, but may cause fuzzy meaning.
Id type
The id type can represent allObject Type. Objects of any class can be assigned to variables of the id type.
When you call a method using a variable of the id type, objective-c performs dynamic binding. Dynamic binding: objective-c will track the class to which the object belongs, judge the class to which the object belongs at runtime, and determine the method to be dynamically called at runtime, instead of determining the method to be called during compilation.
Example program:
# Import
# Import FKPerson. hint main (int argc, char * argv []) {@ autoreleasepool {// defines the id type variable, and assign the FKPerson object to the variable id p = [[FKPerson alloc] init]; // use the p variable to call the say: method. // The program will be dynamically bound at runtime. Therefore, the method [p say: @ hello, crazy iOS handout] of the FKPerson object is actually executed.}