Two. Initialization method

Source: Internet
Author: User

① inheritance can not only guarantee the integrity of the class, but also simplify the code ② simplify the code ③ the public methods and instance variables written in the parent class, subclasses only need to write their own unique instance variables and methods. ④ Inheritance Features: "1" without the parent class is called the Root class, OC root class is nsobject (ancestor) "2" Inheritance of the upper layer: The parent class, the inheritance of the lower layer: Subclass "3" inherits the content: all the instance variables and methods "4" Inheritance is one-way, cannot inherit "5" Inheritance has transitivity: a inherits from the characteristics and behavior of b,b inherited from C,a with B and C "6" If the subclass is not satisfied with the implementation of the parent class method, you can override the parent class's method. The ⑤ overrides three cases of methods inherited from the parent class: "1" is based entirely on the implementation of the sub-category, discarding the contents of the parent implementation of "2" is based entirely on the implementation of the parent class content, there is no sub-class implementation of the content of "3" is the implementation of the method of the parent class, there are subclasses of the implementation of the method of the implementation of ⑥ "1 Find out if this class implements (overrides) The study method, "2" if not, find the parent class study whether the implementation of "3" step-up search, found that the implementation of "4" if NSObject is not implemented, will throw an exception, Causes the Crash⑦ subclass to define an instance variable other than a public instance variable in the parent class 2.super (not a pointer) ① effect: Give super message, can execute the parent class implementation of the method 3.self (pointer) ① system keyword. Self in the method refers to the caller of the current method ②self in the instance method, referring to the object that invokes the current method ③self in the class method, Refers to the current Class 4. Initialization method The primary function of the ① initialization method is to assign an initial value to certain instance variables ② initialization method is used only once for the whole life of an object: The initialization method is to complete the assignment of its instance variables at the initialization stage of an object, and the initialization method is used only once. Example: declaration. H (Instancetype) Initwithname: (NSString *) name; M (instancetype) Initwithname: (NSString *) name Age: ( Nsinteger) Age Score: (cgfloat) score{

if (self = [super init]) {
_name = name;
_age = age;
_score = score;
}
return self;} ③ in its own initialization method, the priority is to send the INIT message to Super, initialize the public variables, initialize the success, and then initialize their own unique variables, to complete the initialization of the instance variables. ④ initialization method Feature "1" initialization method is "-" instance method "2" id or Instancetype type return value "3" begins with int "4" can take 0 to many parameters ⑤ internal implementation: First execute Super initialization method, then initialize the self variable, then return Self⑥ initialization process: "1" uses super to invoke the initialization method of the parent class, which initializes the instance variable that inherits from the parent class "2" returns an address after initialization is completed, which is the address of the object "3" Self is a pointer to his own object. Self Save returned address "4" The return value may be empty. If the return value is empty, nothing is done. The return value is not NULL, Initialize your own instance variable 5. Specify the initialization method ① a class can have more than one initialization method ② Although there can be multiple initialization methods, an object can only use one initialization method ③ Specify the initialization method: The initialization method that will be called regardless of which initialization method is invoked ④ typically put all the operations you want to do at initialization to the specified initialization method , this eliminates the need to write again in each initialization method, simplifying the code, and facilitating the maintenance of the ⑤ selection principle at a later stage: The initialization method with the most common selection of parameters as an example of a specified initialization method: declaration. H (Instancetype) Initwithname: (NSString *) Name

-(Instancetype) Initwithname: (NSString *) name Age: (Nsinteger) age;

-(Instancetype) Initwithname: (NSString *) name Age: (Nsinteger) Age Score: (cgfloat) score; m (instancetype) Initwithname: (NSString *) name Age: (Nsinteger) Age Score: (cgfloat) score{

if (self = [super init]) {
_name = name;
_age = age;
_score = score;
}
return self;
}

-(Instancetype) Initwithname: (NSString *) name Age: (Nsinteger) age{
All basic data type 0, object, class fill nil
return [self initwithname:name age:age score:0];
}

-(Instancetype) Initwithname: (NSString *) name{

return [self initwithname:name age:0 SCORE:0];} 6. The convenience constructor ① encapsulates the object creation process internal implementation: encapsulates the Alloc and initialization operations, creating objects more convenient ② implementation method "1" is the "+" class Method 2 Returns the instance of this type "3" The method name begins with the class name 4 can have 0 to many parameter examples: declaration. H + (ID) Personwithname: (NSString *) name Gender: (NSString *) gender; m+ (ID) personwithname: (NSString *) name Gender: (nsstring *) Gender {return [[Person alloc] initWithName:namegender:gender];} Adjustable? Person *per = [Person personwithname:@ "Frank" gender:@ "male"];

Two. Initialization method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.