Class Factory method
Class methods for quickly creating objects, which we call class factory methods
Class Factory Method Application Scenario
The class factory method is primarily used to allocate storage space to objects and initialize this storage space
Class factory method Usage specification
Specification:
Must be class method +
The method name begins with the name of the class, with the first letter lowercase
There must be a return value, the return value is Id/instancetype
In the class factory method implementation, call the constructor of this class, create an instance object, and return the instance object
The custom class factory method is a specification for Apple, and in general we give a class A custom constructor method and a custom class factory method for creating an object
The attention point of class factory method in Inheritance
After any custom class factory method, create the object in the class factory method must use self to create, do not use the class name to create
The nature of the class
The class is actually an object that will be created when the class is first used
As long as you have a class object, you can create an instance object from the class object in the future
There is an Isa pointer in the instance object that points to creating your own class object
All object methods of the current object are saved in the class object
When a message is sent to an instance object, the corresponding class object is searched based on the ISA pointer in the instance object.
The inheritance of all class objects is the inheritance relationship of the meta-class objects:
Get class objects and class object scenarios
Get Class object
To get a class object using the class method
[Instance object class]; [Class name classes];
Person *P1 = [[Person alloc] init];
Person *P2 = [[Person alloc] init];
A class has only one copy of the class object in memory
Class C1 = [P1 class];
Class C2 = [P2 class];
Class C3 = [Person class];
NSLog (@ "C1 =%p, C2 =%p, C3 =%p", C1, C2, C3);
Application Scenarios for Class objects
Used to create an instance object
Person *P3 = [[C1 alloc] init];
P3.age = 30;
NSLog (@ "%i", p3.age);
Used to invoke class methods
[Person Test];
[C1 test];
Basic concepts of 24-OC class factory methods and Class objects