Objective-c Factory method

Source: Internet
Author: User

the method that the class method uses to create the object is the factory method1. Non-reference factory methodcreate an object and give the property a default value.
// no reference factory method implementation + (student*) student{    return  [[Student alloc]init];}
2. Factory Method2.1. To rely on a parameter initialization method-(ID) initwithage: (int) age;2.2. Create an object and give the property a specified value
// The factory method implementation + (student*) Studentwithage: (int) Age Andsex: (char) Sex andsalary: (double  ) salary{    return  [[Student alloc]initwithage:age andsex:sex andsalary:salary];}
Specification: The method name of the factory method must begin with the class name, note that the prefix is removed, the first letter to the lowercase factory method, not only the use of custom classes, the official class also adhere to this specification

class Student.h

Objective-c Factory method

Related Article

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.