Yesterday we introduced the definition and use of classes in OC: Classes
I. First, let's take a look at the class initialization method.
In Java, we know that each class has a constructor. The initialization method here is similar to the constructor method, but the difference is that Java has a default constructor, when we customize the constructor, this default constructor will be replaced, but it will not be in OC. Pay attention to this.
The following describes how to customize the initialization method:
Person. h
/// Person. h // 03_initdemo /// created by jiangwei on 14-10-10. // copyright (c) 2014 jiangwei. all rights reserved. // # import <Foundation/Foundation. h> @ interface person: nsobject {nsstring * _ name; nsinteger _ age;} // custom initialization method, which must start with init-(ID) initwithname :( nsstring *) name withage :( nsinteger) age; // print information-(void) showinfo; @ end
Here we define an initialization method to initialize and assign values to names and ages.
Note: The names of custom initialization methods in OC must start with init.
Let's take a look at the implementation:
Person. m
/// Person. M // 03_initdemo /// created by jiangwei on 14-10-10. // copyright (c) 2014 jiangwei. all rights reserved. // # import "person. H "@ implementation person-(ID) initwithname :( nsstring *) Name withage :( nsinteger) Age {// fixed writing method, you can back it down ~~ // Call the initialization method of the parent class self = [Super init]; If (self! = Nil) {_ name = Name; _ age = age;} return self;}-(void) test {nslog (@ "test") ;}- (void) showinfo {[self test]; // call your own method nslog (@ "name is % @ and age is % d", _ name, _ age);} @ end
We can see that the type returned by the initialization method is ID.
ID type, It can be considered that it is equal to the void * in C language, this type is often used in OC, And there is
SelfThe self keyword is equivalent to the this keyword in Java. It refers to the current class object and the super keyword.
SuperThe same is true for parent class objects. Another one is
NilIt is equivalent
Null, The concept of a null object, used for judgment.
At the same time, remember:
Templates for custom initialization methods in later OC:
// Fixed writing method, which can be memorized ~~ // Call the initialization method of the parent class self = [Super init]; If (self! = Nil) {_ name = Name; _ age = age;} return self;
There is nothing to say about it. It is a template, so Beginners should stick it back. However, if it is used more often, they will naturally remember it.
The following describes how to use the initialization method:
Main. m
//// Main. M // 03_initdemo /// created by jiangwei on 14-10-10. // copyright (c) 2014 jiangwei. all rights reserved. // # import <Foundation/Foundation. h> # import "person. H "int main (INT argc, const char * argv []) {@ autoreleasepool {// The initialization method can only call one person * person = [[person alloc] initwithname: @ "jiangwei" withage: 22]; [person showinfo]; person = [[person alloc] init]; [person showinfo]; // ID type, similar to void * // but the ID type is generally not used this way. It is generally used as the return value of the method and the input parameter type ID person1 = [[person alloc] initwithname: @ "huangdaoyang" withage: 23]; [person1 showinfo];} return 0 ;}
We used our custom method to instantiate a person object and call the showinfo method to print the result. Here we will also find that the init method of person can still be used, this is different from Java. In fact, as mentioned in the previous article, the init method is the nsobject parent class. We can guess that the logic function of this method is initialization, therefore, this method is used when you customize the initialization method. Of course, we can define multiple initialization methods. When we introduce the foundation framework, we will find that the classes provided by the system have many custom initialization methods.
Running result:
Ii. Take a look at the dot syntax in oC
The dot syntax in OC is actually nothing high-end, but it is different from the point in Java that uses Object variables to directly access public variables. So here we will explain it separately:
Point syntax can only apply to the set/get method. If there is no corresponding set/get method, it cannot use. syntax.
The get method in OC is not the same as that in Java. In oC, if a method has a return value, it can be considered as a get method. In Java, A get method is called if it is like getxxx.
Here is an example:
Person. h
/// Person. h // 04_usedit // created by jiangwei on 14-10-11. // copyright (c) 2014 jiangwei. all rights reserved. // # import <Foundation/Foundation. h> @ interface person: nsobject {// point syntax can only be used for the set/get method. If no corresponding set/get method exists, it cannot be used. the get method in the syntax // here is different from other languages, as long as the return value is the get method nsstring * _ name; nsinteger _ age;}-(void) setname :( nsstring *) Name; -(void) setage :( nsinteger) age;-(nsstring *) Name;-(nsinteger) age; // showinfo is also a get method (nsstring *) showinfo; // only defined, not implemented, so when calling [person test], no error will be reported during compilation // but an error will be reported during running, during compilation, the system only determines whether the method is defined, and during running, it is necessary to determine whether the method has been implemented-(void) test; @ end
Two attributes are defined in {}. Note that they all start with an underscore. Then define the get/Set Method for these two attributes, so that we can use the dot syntax to access these two attributes.
At the same time, it should be noted that the showinfo method is also a get method, because it has another return value type.
Therefore, the dot syntax in OC has two conditions:
1. Must be an attribute
2. There is a corresponding get/Set Method
Let's take a look at the implementation below:
Person. m
/// Person. M // 04_usedit /// created by jiangwei on 14-10-11. // copyright (c) 2014 jiangwei. all rights reserved. // # import "person. H "/** # import and # include differences 1. when we use # include twice in the code, an error will be reported: Because # include is equivalent to copying the Declaration content in the header file, so we will report a duplicate definition error but use # import twice, no error is reported, so he can solve the problem of repeated import. He will make a judgment and will not import it once. */@ implementation person-(void) setname :( nsstring *) name {_ name = Name; nslog (@ "setname is execute");}-(void) setage :( nsinteger) Age {// note that, if the global variable we define is age rather than _ age, // here we cannot use age = age to assign values //, but we cannot use self. age = age, because self. age is equivalent to [self setage: Age], and an infinite loop will occur. // Therefore, when defining global variables, we usually add an underscore to distinguish _ age = age; nslog (@ "setage is execute");}-(nsstring *) name {nslog (@ "getname is execute"); Return _ name;}-(nsinteger) age {nslog (@ "getage is execute"); Return _ age;}-(nsstring *) showinfo {nslog (@ "showinfo is execute"); Return _ name ;} @ end
We can see the implementation of the get/set method here, and see the above comment:
We cannot use self. Age = age here, because self. Age is equivalent to [self setage: Age], so there will be an endless loop.
This is very different from Java. in Java, we can use this. age = age, and this is often done, but not in OC, the reason is very simple, because the point syntax in OC is equivalent to calling the set/get method, if you call the set/get method in the Set/get method, an endless loop will occur. So pay attention to this.
Another thing to note is that it was just in person. h defines a test method, but in person. M is not implemented. Compiling in OC will not report errors, but errors will be reported during running.
Let's take a look at how to use the dot Syntax:
Main. m
//// Main. M // 04_usedit /// created by jiangwei on 14-10-11. // copyright (c) 2014 jiangwei. all rights reserved. // # import <Foundation/Foundation. h> # import "person. H "int main (INT argc, const char * argv []) {@ autoreleasepool {person * person = [[person alloc] init]; // The Point syntax here calls the set method, which is equivalent to [person setname: @ "jiangwei"]; person. name = @ "jiangwei"; person. age = 22; // The get method called by the dot syntax here, equivalent to nsstring * name = [person name]; nsstring * name = person. name; nslog (@ "name is % @", name); name = person. showinfo; nslog (@ "name is % @", name); // call the test method [person test];} return 0 ;}
Here we can use the dot syntax for value assignment and value operations, which is equivalent to calling their get/set method.
Running result:
Here, we need to note that an error is reported. This error is also a common mistake in the subsequent development process, that is, this method is not implemented. We are in person. the test method is defined in h, but not in person. M implementation, in Main. m.
Summary
Today, I briefly introduced the definition of initialization methods in OC and the use of point syntax. The initialization method is similar to the constructor method in Java. The dot syntax is the get/set reduction version.
OC learning --- class initialization method and point syntax usage