Class extension in OC

Source: Internet
Author: User

The extension of OC classes is summarized as follows:

There are 4 in total:

1. subClass

Purpose: You can use class inheritance to add the variables and methods of the parent class.

Statement: In the. h file

@ Interface Student: Person

2. Category

Function: you can add a parent class, but cannot define variables. It is often used to extend the parent class without knowing the source code.

Syntax: In the. h and. m files, the file name is in the format of "parent class name + category name.

After @ interface @ implementation, "parent class name (category name )"

3. Extension

Purpose: Define private methods. You can hide methods that are not published. Mostly used to hide some intermediate steps.

Written in: implemented before @ implementation in the. m file

@ Interface Person () {NSString * _ age ;}
-(Void) aa;
@ End in this way, the private method is defined.
4. protocol
Purpose: implement methods similar to multiple inheritance. A class complies with multiple protocols. Statement: the Protocol only has. H files, and the method is defined. @ Required // required by default
-(Void) method1;

-(Void) method2;

@ Optional // optional

-(Void) method3;

Currently, we will continue to add more.

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.