Objective-C factory method, objective-c Factory
The method used to create an object is factory method 1. The no-parameter factory method creates an object and gives the property a default value.
// Implementation of the factory method without parameters + (Student *) student {return [Student alloc] init];}
2. Factory method 2. 1. dependent on the initialization method with parameters-(id) initWithAge :( int) age; 2. Create an object and give the attribute a specified value
// Implementation of the factory method with parameters + (Student *) studentWithAge :( int) age andSex :( char) sex andSalary :( double) salary {return [[Student alloc] initWithAge: age andSex: sex andSalary: salary];}
Standard: The method name of the factory method must start with the class name. Note that the prefix should be removed. The first letter must be the lower-case factory method. Not only do custom classes are used, but official classes also comply with this standard.
Class Student. h