Polymorphism in C ++ (two types ):
Compile-time matching: function overload. Different function call matching is implemented based on the name, type, and number of member functions;
Runtime matching: virtual functions, specifically by declaring the member function as virtual when the parent class or abstract class is declared, and then the subclass inherits and implements the function, by assigning the subclass object address to the pointer of its parent class (note: the abstract class cannot be instantiated, that is, the object cannot be defined, but the pointer and reference to the class can be defined ), call the virtual function pointed to by the parent class pointer after the assignment to call different sub-classes for different implementations of the virtual function.
In C ++, virtual is used for Polymorphism when running.
Use Protocol to implement "polymorphism" in Obiective-C. For details, see the following example (the example is from objective-C beginner's guide).
Printing. h
@protocol Printing-(void) print;@end
Fraction. h
#import <Foundation/NSObject.h>#import "Printing.h"@interface Fraction: NSObject <Printing, NSCopying> { int numerator; int denominator;}-(Fraction*) initWithNumerator: (int) n denominator: (int) d;-(void) setNumerator: (int) d;-(void) setDenominator: (int) d;-(void) setNumerator: (int) n andDenominator: (int) d;-(int) numerator;-(int) denominator;@end
Fraction. m
#import "Fraction.h"#import <stdio.h>@implementation Fraction-(Fraction*) initWithNumerator: (int) n denominator: (int) d { self = [super init]; if ( self ) { [self setNumerator: n andDenominator: d]; } return self;}-(void) print { printf( "%i/%i", numerator, denominator );}-(void) setNumerator: (int) n { numerator = n;}-(void) setDenominator: (int) d { denominator = d;}-(void) setNumerator: (int) n andDenominator: (int) d { numerator = n; denominator = d;}-(int) denominator { return denominator;}-(int) numerator { return numerator;}-(Fraction*) copyWithZone: (NSZone*) zone { return [[Fraction allocWithZone: zone] initWithNumerator: numerator denominator: denominator];}@end
Complex. h
#import <Foundation/NSObject.h>#import "Printing.h"@interface Complex: NSObject <Printing> { double real; double imaginary;}-(Complex*) initWithReal: (double) r andImaginary: (double) i;-(void) setReal: (double) r;-(void) setImaginary: (double) i;-(void) setReal: (double) r andImaginary: (double) i;-(double) real;-(double) imaginary;@end
Complex. m
#import "Complex.h"#import <stdio.h>@implementation Complex-(Complex*) initWithReal: (double) r andImaginary: (double) i { self = [super init]; if ( self ) { [self setReal: r andImaginary: i]; } return self;}-(void) setReal: (double) r { real = r;}-(void) setImaginary: (double) i { imaginary = i;}-(void) setReal: (double) r andImaginary: (double) i { real = r; imaginary = i;}-(double) real { return real;}-(double) imaginary { return imaginary;}-(void) print { printf( "%_f + %_fi", real, imaginary );}@end
Main. m
#import <stdio.h>#import "Fraction.h"#import "Complex.h"int main( int argc, const char *argv[] ) { // create a new instance Fraction *frac = [[Fraction alloc] initWithNumerator: 3 denominator: 10]; Complex *comp = [[Complex alloc] initWithReal: 5 andImaginary: 15]; id <Printing> printable; id <NSCopying, Printing> copyPrintable; // print it printable = frac; printf( "The fraction is: " ); [printable print]; printf( "\n" ); // print complex printable = comp; printf( "The complex number is: " ); [printable print]; printf( "\n" ); // this compiles because Fraction comforms to both Printing and NSCopyable copyPrintable = frac; // this doesn't compile because Complex only conforms to Printing //copyPrintable = comp; // test conformance // true if ( [frac conformsToProtocol: @protocol( NSCopying )] == YES ) { printf( "Fraction conforms to NSCopying\n" ); } // false if ( [comp conformsToProtocol: @protocol( NSCopying )] == YES ) { printf( "Complex conforms to NSCopying\n" ); } // free memory [frac release]; [comp release]; return 0;}
Output
The fraction is: 3/10The complex number is: 5.000000 + 15.000000iFraction conforms to NSCopying