First, the construction method
(i) Call to construct method
Complete creation of an available object: Person *p=[person new];
The inner part of the new method calls two methods to accomplish 2 things, 1) Use the Alloc method to allocate storage space (return allocated objects), 2) Use the Init method to initialize the object.
The new method can be opened as follows:
1. Call the class method +alloc allocate storage space and return uninitialized objects
Person *p1=[person alloc];
2. Call object method-init to initialize, return the object itself
Person *P2=[P1 Init];
3. The above two processes are integrated into one sentence:
Person *p=[[person alloc] init];
Description: The Init method is the constructor method, the method used to initialize the object, note that this is an object method, and a minus sign begins. After the default initialization is complete, all member variables have a value of 0.
(ii) Code Examples of construction methods
Need 1, if I need to let each object create the initial value is 10, instead of 1, what should I do?
Demand 2, let students inherit human, ask students to initialize the object, the age is 10, the study number is 1, how to do?
(iii) Construction method use note
(1) A subclass has member variables that include its own member variables and member variables inherited from the parent class, and the member variables that inherit from the parent class should be initialized first when overriding the constructor method.
(2) Principle: Initialize the parent class first, and then initialize the subclass.
(3) The purpose of overriding the construction method: In order for the object method to be created, the member variable will have some fixed value.
(4) Note: #1先调用父类的构造方法 [super Init]; #2再进行子类内部成员变量的初始化.
Ii. Custom Construction methods
(i) Specification of custom construction methods
(1) must be an object method, starting with a minus sign
(2) The return value is typically an ID type
(3) method names usually start with Initwith
(ii) Code implementation of custom construction methods
Declaration of the Person class, which declares a custom construction method of two receive parameters
Implementation of the Person class
Student inherits from the person class and declares a constructor method that receives three parameters
Implementation of the Student class
Test the main program
(iii) Use of custom construction methods note
(1) Do your own thing
(2) The method of the parent class is given to the parent class's method to handle, and the subclass's method handles the subclass's own unique properties
Third, classification
(i) basic knowledge of classifications
Concept: Category classification is the language specific to OC and depends on the class.
The function of classification: Add some methods to the class without changing the original class content.
Add a Category:
File structure diagram:
Add a method to the taxonomy
Implementation of study method
Test procedure:
(ii) Attention to the use of classifications
(1) Classification can only add methods (including class methods and Object methods), cannot increase member variables
(2) The member variables in the original class can be accessed in the implementation of the classification method;
(3) Classification can be re-implemented in the original class method, but will overwrite the original method, resulting in the original method can no longer use (warning);
(4) Priority of method invocation: Classification---The original class--parent class, if it contains more than one classification, then the final participation in the compilation of the classification takes precedence;
(5) In many cases, it is often possible to add categories to the system's own classes, such as NSObject and nsstring, because sometimes the system classes may not meet our requirements.
(6) In large-scale applications, the corresponding function is usually written in a classification, can have unlimited classification, the original class to expand, General sub-module write, a module a classification.
(iii) Classification programming exercises
(1) Adding a class method to the NSString class to calculate the number of Arabic numerals in a string object;
(2) Add an object method to the NSString class to calculate the number of Arabic numerals in the current string object;
Declaration of methods in classification
Implementation of methods in classification
Test procedure:
In-depth study of class Iv.
(a) The nature of the class
The class itself is also an object, an object of class type, or "Class object".
Definition of class type:
Typedef struct obj class *class;
The class name represents the class object, and each class has only one class object.
Create a person class with class
Create an object of person type with person
Person *p=[[person alloc] init];
There are two ways to get a class object in memory:
(1) class C=[p claa];//pointer to the object of the class.
(2) class C1=[person class];//Call the class method using the classes name
Note: C and C1 print out the same address, class c2=[p Claa]; You can prove that all objects share a class method.
(ii) Loading and initialization of classes
Test procedure:
1. When the program starts, it loads all classes and classifications in the project, and the +load method of each class and classification is called once after loading;
2. When a class is used for the first time, the +initialize method of the current class is invoked;
3. Load the parent class first, then load the subclass (call the +load method of the parent class first, then call the +load method of the subclass, and finally call the +load method of the taxonomy), initialize the parent class first, then initialize the subclass (call the +initialize method of the parent class first, and then call the subclass's + Initialize method).
4. Note: At initialization time, if the +initialize method is overridden in the taxonomy, the parent class is overwritten.
5. Override the +initialize method to listen for class usage.
Objective-c language-in-depth and classification of construction methods and classes