[Dark Horse programmer] Objective-c Object-oriented classification

Source: Internet
Author: User

What is classification

Classification can be used to extend the method of a class without modifying the code of the original class.

Let's look at a simple example:

We have a class Calculator we have added and subtracted methods for it:

#import "Foundation/foundation.h"@interfaceCalculator:nsobject//Plus-(int) Plus: (int) Num1 and: (int) num2;// Less-(int) Less: (int) Num1 and: (int) num2;@end@implementationCalculator-(int) Plus: (int) Num1 and: (int) num2{returnNUM1 +num2;}-(int) Less: (int) Num1 and: (int) num2{returnNUM1-num2;}@end

We put the implementation and declarations of the above Calculator in Calculator.h and calculator.m , and the demand now is that we want to extend this calculator class so that it can multiply and divide, But because of the division of labor, the multiplication and division were developed separately for Tom and Jerry , but if Tom and Jerry modified Calculator.h at the same time and calculator.m can cause a variety of conflicts (actual development might use SVN or git to automate these issues, but the assumptions here are much simpler than the actual requirements), so we want Tom and Jerry will need to use the classification of OC to create different files for development.

Tom:

  #import  <calculator.h> @interface   Calculator (Tom) -(int ) Multiplied: (int ) Num1 and: (int  ) num2;   @end   @implementation   Calculator (Tom) -(int ) multiplied: ( Span style= "color: #0000ff;" >int ) Num1 and: (int  ) num2{ return  num1 * num2;}   @end  

Note: In order to save space, write the Declaration and implementation together, we still believe that the above code is placed in the calculator+tom.h and the CALCULATOR+TOM.M , the same as the same .

Jerry:

#import <Calculator.h>@interface  Calculator (Jerry)-(double) except: ( int) NUM1 and: (int) num2; @end @implementation Calculator (Jerry)-(double) except: (int) NUM1 and: (int) num2{   return num1/ num2;} @end

When we need to use them, we introduce Calculator.h, calculator+tom.h, Calculator+jerry.h, respectively:

#import "Calculator.h"#import "calculator+tom.h"#import "Calculator+jerry.h"intMain () {Calculator*calculator = [CalculatorNew]; [Calculator Plus:3And:4];//Output:7[Calculator Less:3And:4];//Output: 1[Calculator multiplied:3And:4];//Output:12[Calculator except:3And:4];//output:0.75  return 0;}
Use Note:
    • Classification can only be extended to methods, not to increase the definition of member variables
    • Methods in classes and classifications that have the same name will overwrite the same method as the original class, causing the method of the original class to be unavailable
    • The order of the calls is: The method priority of the classification is the highest, then the method of the original class priority
    • If a class has multiple classifications, the order in which it is compiled is the priority of the method invocation

[Dark Horse programmer] Objective-c Object-oriented classification

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.