"OC" constructor and classification, and "oc" Constructor

Source: Internet
Author: User
Tags arabic numbers

"OC" constructor and classification, and "oc" Constructor
I. Constructor(1) Call of Constructor

Create an available object: Person * p = [Person new];

The new method is actually divided into two steps to create an object:

1) Use the + alloc method to allocate storage space (return the allocated object );

2) use the-init method to initialize the object.

You can split the new method as follows:

1. Call the class method + alloc to allocate storage space and return uninitialized objects.

Person * p1 = [person alloc];

2. Call the object method-init to initialize and return the object itself.

Person * p2 = [p1 init];

3. The above two processes are integrated into one sentence:

Person * p = [[Person alloc] init];

Note: The init method is the constructor method used to initialize objects. Note that this is an object method starting with-Minus signs. After Initialization is complete by default, the value of all member variables is 0.

(2) sample code for Constructor

Requirement 1: What should I do if I want to create an initial value of 10 instead of 1 for each object?

  

Requirement 2: let students inherit from humans. After the student object is initialized, the age is 10 and the student ID is 1. What should I do?

  

(3) constructor

(1) The member variables owned by the subclass include their own member variables and member variables inherited from the parent class, when rewriting the constructor, initialize the member variables inherited from the parent class first.

(2) Principle: Initialize the parent class before initializing the Child class.

(3) Purpose of rewriting constructor: to create an object method, the member variables have fixed values.

(4) Note: #1 first calls the constructor of the parent class [super init]; #2 and then initializes the member variables in the subclass.

Ii. Custom Constructor (1) specifications of custom Constructor

(1) it must be an object method, starting with a minus sign

(2) return value is generally id type

(3) method names generally start with initWith

(2) code implementation of custom Constructor

The Person class declaration, which declares the custom constructor of two receiving Parameters

Person class implementation

  

Student inherits from the Person class and declares a constructor that receives three parameters.

  

Implementation of Student Class

  

Test main program

  

(3) usage of custom Constructor

(1) Do your own thing

(2) The method of the parent class is handed over to the method of the parent class for processing. The method of the subclass processes the attributes unique to the subclass.

Iii. Classification (1) Basic knowledge of classification

Concept: Category is a language specific to OC and relies on classes.

Classification: adds methods for classes without changing the original class content.

Add a category:

  

Add a study method to the category:

Implementation of the study method:

Test procedure:

(2) usage of classification

(1) only methods (including class methods and object methods) can be added for classification, and member variables cannot be added.

(2) Access member variables in the original class in the implementation of classification methods;

(3) methods in the original class can be re-implemented in the classification, but the original method will be overwritten, resulting in the failure to use the original method (warning );

(4) method call priority: Class-> original class-> parent class. If there are multiple classes, the classification involved in compilation takes precedence;

(5) In many cases, we often add categories to the classes that come with the system, such as NSObject and NSString, because sometimes the system classes may not meet our requirements.

(6) In large-scale applications, the corresponding functions are generally written into a category, and there can be unlimited categories. The original classes are extended. Generally, they are written in modules and each module is classified.

(3) classification exercises

(1) Add a class method to the NSString class to calculate the number of Arabic numbers in a string object;

(2) Add an object method to the NSString class to calculate the number of Arabic numbers in the current String object;

Declaration of methods in classification

Implementation of Classification Methods

Test procedure:

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.