For learning iOS, attack ... OC Multi-State exercise

Source: Internet
Author: User

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

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.