In C #, we use interfaces to implement polymorphism. For example, the interface iob defines a method F. There are two classes A and B that implement the IOB interface.
Iob item = new ();
Item. F (); // The execution is a.f ();
Item = new B ();
Item. F (); // The executed B. F ();
In objective-C, the meaning of the interface is very different from that of C #, and cannot be used in this way.
So how can we achieve similar results. This is a special type ID.CodeSegment,Note: Both fraction and complex contain the print method.
# Import "fraction. h"
# Import "complex. h"
Int main (INT argc, char * argv [])
{
NSAID utoreleasepool * Pool = [[NSAID utoreleasepool alloc] init];
Id datavalue; // Defines an ID variable.
Fraction * F1 = [[fraction alloc] init];
Complex * C1 = [[complex alloc] init];
[F1 setto: 2 over: 5];
[C1 setreal: 10.0 andimaginary: 2.5];
// First datavalue gets a fraction
Datavalue = F1;
[Datavalue print]; // Call the print method of Fraction
// Now datavalue gets a complex number
Datavalue = C1;
[Datavalue print]; // Call the print method of Complex
[C1 release];
[F1 release];
[Pool drain];
Return 0;
}