IOS Classification and inheritance

Source: Internet
Author: User

Categories and Inheritance (Inherit) have the same functionality in IOS, but there are differences in some details about the similarities and differences.

Classification can add new methods to this class without knowing the source code of the System class. Categories can only be used to add methods, and cannot add member variables. The system is considered to be part of the class type by the method of increasing the classification . Like what:

  1   #import  <uikit/uikit.h>< Span style= "color: #008080;" > 2   3   @interface   UIImage (WB)   4   5  /*   6    7  */  8  + (UIImage *) Resizedimagewithname: (nsstring *) name;   9  10  11   @end  

UIImage is a class of systems that can still be categorized to add new methods to the class without knowing the source code of the UIImage class. And the method becomes part of that type. If other classes inherit from the UIImage class, the subclasses of the UIImage class all have the Resizedimagewithname method.

From this perspective, there is a big difference between inheritance and classification: subclasses inherit from the parent class, and the added methods in the subclass do not become part of the parent class . That is, if Class A inherits from UIImage, a resizedimage function is added to Class A, and the parent class UIImage cannot use the function, and when there are new classes such as Class B inherits from UIImage, there is no resizedimage function in class B.

However, inheritance can add both methods and member variables. Of course, the member variables that are added are also limited to being used in subclasses.

IOS Classification and inheritance

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.