directory of this document
- Knowledge Review
- One, custom construction method
- Second, description method
Description: This objective-c topic is a prelude to learning iOS development, and to enable programmers with experience in object-oriented language development to quickly get started with objective-c. If you do not have programming experience, or are not interested in objective-c, iOS development, please ignore. Before studying this topic, it is recommended to study the C language topic first.
Back to top of knowledge review
In the 5th lecture, we have described how to define classes and create and initialize objects, such as the student class.
1.student.h
1 #import <foundation/foundation.h>2 3 @interface student:nsobject {4 int _age;5}6-(void) Setage: (int) age;7 -(int) age;8 @end
2.student.m
1 #import "Student.h" 2 3 @implementation Student 4-(void) Setage: (int) Age {5 _age = age; 6} 7-(int.) Age {8
return _age; 9}10 @end
3. Create a student object in the main function
1 #import "Student.h" 2 3 int main (int argc, const char * argv[]) 4 {5 6 @autoreleasepool {7 Student *st u = [[Student alloc] init]; 8 9 stu.age = 10;10 [stu release];12 }13 return 0;14}
* In line 7th, call Student's Alloc method to allocate memory, and then call the Init method to initialize the object
* Methods for initializing objects like Init, which we can call "construction method"
Back to top one, custom construction methods
The default constructor is the Init method, which does not receive any parameters. Therefore, in the actual development, in order to facilitate, will often customize the construction method.
Next, customize a constructor method that can pass in an age parameter to initialize the student object
1. Add a method declaration in Student.h
-(ID) initwithage: (int) age;
* The method name of the constructor method usually starts with Init, and the return value is the same as the Init method for the ID type, and the ID can represent any OC object
* This constructor takes an age parameter of type int, in order to set the value of the member variable _age when initializing the student object
2. Implementing a construction method in STUDENT.M
1-(ID) initwithage: (int) Age {2 * = [Super Init];3 if (self! = nil) {4 _age = age;5 }6 return self ; 9 3
* Like Java, the constructor method calls the parent class's constructor first, and on line 2nd calls the parent class's Init method, which returns the initialized student object, which assigns the return value to Self,self, which represents the student object itself.
* The 3rd to 5th line means: If self is not nil, that is, the initialization succeeds, the member variable _age is assigned
* Finally returns self after initialization, the entire construction method is over.
3. Simplified Construction method
Because of the syntactic characteristics of C and OC, we can simplify the construction method by simplifying line 3rd first
1-(ID) initwithage: (int) Age {2 * = [Super Init];3 if (self) {4 _age = age;5 }6 return self;7}
* The IF (self) of line 3rd is equivalent to if (Self!=nil)
* You can also combine lines 2nd and 3 to continue simplifying
1-(ID) initwithage: (int) Age {2 if (self = [super init]) {3 _age = age;4 }5 return self;6}
* The general meaning of Line 2nd is: First call the parent class's constructor init, then assign the return value to self, and then determine if self is nil
* This is how the construction method is written
4. Calling the constructor method
1 Student *stu = [[Student alloc] initwithage:10];2 3 NSLog (@ "Age is%i", Stu.age); 4 5 [Stu release];
* In line 1th, the constructor method Initwithage is called and passed in 10 as a parameter, so the student object's member variable _age becomes 10
* Print the Student member variable _age on line 3rd to print the result:
2013-04-19 21:36:47.880 construction method [448:303] age is 10
Back to top two, description method 1. NSLog Review
As we all know, we can use the NSLog function to output strings and some basic data classes
1 int age = 11;2 NSLog (@ ' age is%i ', age);
* The%i of line 2nd will output an integer data, and the right variable age will be used instead of the%i position.
* Output Result:
2013-04-19 21:43:47.674 construction method [483:303] age is 11
2.NSLog Output OC Object
In fact, in addition to the output of the basic data type, the NSLog function can also output any OC object
1 Student *stu = [[Student alloc] initwithage:10];2 3 NSLog (@ "%@", Stu); 4 5 [Stu release];
* In line 3rd with the NSLog function output Stu object, note the left of the format character%@, later want to output OC object, you have to use%@ this format character
* Once the NSLog function is found to output an OC object with%@, the object's description method is called (The return value of this method is the NSString type, the string type in OC), and the string returned by the description method is replaced by the%@ The location of the output
* The default implementation of the description method is to return the format:< class name: The memory address of the object, so the output of the above code is:
2013-04-19 21:46:49.896 construction method [492:303] <Student:0x100109910>
Student is the class name, 0x100109910 is the memory address of the object
* Note that%@ can only be used for output OC objects and cannot output other types such as struct body
* People with Java development experience should be able to feel that the description method in OC is the ToString method in Java
3. Overriding the Description method
The default implementation of the description method is to return the class name and the memory address of the object, so that using NSLog to output the OC object is not very significant because we do not care about the memory address of the object, but rather the value of some variables inside the object. Therefore, the description method is often overridden to override the default implementation of the Description method
For example, overriding the student description method, returning the value of the member variable _age
1-(NSString *) Description {2 return [NSString stringwithformat:@ "age=%i", _age];3}
* In line 2nd, a static method called the NSString class is invoked stringWithFormat initializes a string object and returns the string
* If you're going to use NSLog, then you should be able to understand what the 2nd line of method parameters means.
* If _age is 10, then the string returned by the description method is @ "age=10"
* Some people may find it strange that the student object created before is needed to be freed, why is the string object created here not freed? To fully understand this problem, we need to understand the memory management of OC, here we do not do a detailed discussion, there will be chapters in detail to discuss memory management. You can first remember a rule: Normally, the object returned by the static method is not released manually.
* After rewriting the description method, execute the following code again
1 Student *stu = [[Student alloc] initwithage:10];2 3 NSLog (@ "%@", Stu); 4 5 [Stu release];
The output is:
2013-04-19 22:09:56.625 Tectonic method [531:303] age=10
Traps for 4.description methods
Do not use%@ and self in the description method at the same time, the following notation is wrong:
1-(NSString *) Description {2 return [NSString stringwithformat:@ "%@", self];3}
The 2nd line uses both%@ and self, which represents the description method to invoke self, and therefore eventually causes the program to go into a dead loop, calling the description method
"Objective-c" 07-Custom construction methods and Description methods