Printer to achieve polymorphism. Enter 1, using a color printer. Enter 2, use a black and white printer
(a) Main function:
#import <Foundation/Foundation.h>
#import "Person.h"
#import "Printer.h"
#import "BlackPrinter.h"
#import "ColorPrinter.h"
int Main (int argc, const Char * argv[]) {
person *person = [[ person alloc] init];
Printer *printer1 = [[blackprinter alloc] init];
Printer *printer2 = [[colorprinter alloc] init];
Nsinteger A;
NSLog(@ " input 1 and 2 swap Printers ");
scanf ("%ld", &a);
if (A = = 1) {
[Person print:p rinter1];
}
Else
[Person print:p rinter2];
return 0;
}
Two Printer class
#import <Foundation/Foundation.h>
@interface Printer: nsobject
-(void) Doprint;
@end
#import "Printer.h"
@implementation Printer
-(void) doprint{
NSLog(@ " printer has been printed ");
}
@end
Three Person class
#import <Foundation/Foundation.h>
#import "Printer.h"
@interface Person: NSObject
-(void) Print: (Printer *) Printer;
@end
#import "Person.h"
@implementation Person
-(void) Print: (Printer *) printer{
[Printer doprint];
}
@end
Four Colorprinter class
#import "Printer.h"
@interface Colorprinter: Printer
@end
#import "ColorPrinter.h"
@implementation Colorprinter
-(void) doprint{
NSLog(@ " color printer for printing ");
}
@end
Five Blackprinter is similar to the Colorprinter class, omitted here.
This solves this simple polymorphic problem
This article is from the "to learn Ios_ attack" blog, please be sure to keep this source http://10577665.blog.51cto.com/10567665/1679567
For learning iOS, attack ... OC Multi-State exercise