Categories of Object-C

Source: Internet
Author: User
Tags mul

Categories allows you to add new methods for a class (you cannot add new data members), instead of using inheritance methods. In addition, you can do this even if there is no implementation source code for a class. Example:
I. Example 1:Add several math methods for Fraction without using the fraction source code.
1. Code:
Put the previously compiled fraction. O and fraction. h in the same directory as the following file. For example, here is the fractionmath directory.
// 1. fractionmath. h
# Import "fraction. H"

@ Interface fraction (math)
-(Fraction *) Add: (fraction *) F;
-(Fraction *) Mul: (fraction *) F;
-(Fraction *) Div: (fraction *) F;
-(Fraction *) Sub: (fraction *) F;
@ End
// 2. fractionmath. m
# Import "fractionmath. H"

@ Implementation fraction (math)
-(Fraction *) Add: (fraction *) f {
Return [[fraction alloc] initwithnumerator: numerator * [f denominator] +
Denominator * [f numerator]
Denominator: denominator * [f denominator];
}

-(Fraction *) Mul: (fraction *) f {
Return [[fraction alloc] initwithnumerator: numerator * [f numerator]
Denominator: denominator * [f denominator];

}

-(Fraction *) Div: (fraction *) f {
Return [[fraction alloc] initwithnumerator: numerator * [f denominator]
Denominator: denominator * [f numerator];
}

-(Fraction *) Sub: (fraction *) f {
Return [[fraction alloc] initwithnumerator: numerator * [f denominator]-
Denominator * [f numerator]
Denominator: denominator * [f denominator];
}
@ End

// 3. Main. m
# Import <stdio. h>
# Import "fraction. H"
# Import "fractionmath. H"

Int main (INT argc, const char * argv []) {
// Create a new instance
Fraction * frac1 = [[fraction alloc] initwithnumerator: 1 denominator: 3];
Fraction * frac2 = [[fraction alloc] initwithnumerator: 2 denominator: 5];
Fraction * frac3 = [frac1 Mul: frac2];

// Print it
[Frac1 print];
Printf ("*");
[Frac2 print];
Printf ("= ");
[Frac3 print];
Printf ("\ n ");

// Free memory
[Frac1 release];
[Frac2 release];
[Frac3 release];

Return 0;
}

2. Compile and run:
Gcc-fconstant-string-class = nsconstantstring-C main. M-I/gnustep/system/library/Headers
Gcc-C fractionmath. M-I/gnustep/system/library/Headers
GCC main. O fraction. O fractionmath. O-o main-L/gnustep/system/library/libraries/-lobjc-lgnustep-Base

$./Main.exe
1/3*2/5 = 2/15

3. Description:
(1) fraction. O and fraction. h must be placed in the same directory of the code before they can be referenced. Otherwise, the fraction class cannot be found during the link.

Ii. Example 2:In C ++ (or Java), you can use private/protected/public to restrict the access anti-attribute of class methods. However, in object-C, there is no such rule (some are only restrictions on member variables). Now, you can use categories to implement this function. Example:

1. Code:
// 1. classa. h
# Import <Foundation/nsobject. h>

@ Interface classa: nsobject

-(Void) print;
@ End

// 2. classa. m
# Import "classa. H"

@ Implementation classa

// Declaration in. H. This is a public method.
-(Void) print {
Printf ("this is a public Method \ n ");
}
@ End

// Private Method
@ Interface myclass (private)
-(Void) privatemethod;
@ End

@ Implementation myclass (private)
-(Void) privatemethod {
Printf ("this is a private Method \ n ");
}
@ End

// 3. Main. m
# Import <stdio. h>
# Import "classa. H"

Int main (INT argc, const char * argv []) {
// Create a new instance
Classa * A = [[classa alloc] init];

[A print];
Printf ("\ n ");

// The following function will cause compilation errors because the function is declared in. M and cannot be accessed externally. This indirectly implements the private function.
// [A privatemethod];
Return 0;
}

Ii. Compile and run (omitted)
Iii. Description:

(1) The print function declared in. h can be considered public;
(2) In. m, it is declared in the "@ interface myclass (private)" method and cannot be accessed externally. Therefore, the private function is indirectly implemented.
(3) Of course, you can change "private" to other words, such as "aaaaaaa;

(4) For proportion 1, the key difference is that this statement is in. h. M declares that, if it is the former, it can be accessed externally. The original object is a class Extension function, as in Example 1; if it is in. m.

(5) from another perspective, when accessing a class, you need to import. h header file, if. if there is no function declaration in the H file, a compilation error will occur, which is easy to understand.

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.