[OC learning-14] What is polymorphism? View the effect of writing the parent class method in the subclass.

Source: Internet
Author: User

What is polymorphism?

Official statement: different implementation methods for the same interface. In fact, the big vernacular is (taking the parent class and the Child class as an example, it can actually not be limited to this relationship): the parent class has a method (function ), this method (function) is derived from many subclasses, but the content of each method (function) is different. Of course, the same method means the same method name, return value type, and number of parameters.


Directly, for example, we create a parent class asshape and have a draw method to derive the three subclasses ascircle. asrectangle and astriangle each have a draw. Of course, the content in each draw is different, is to output a sentence.

(1) create a parent class first:

// Asshape. h file # import <Foundation/Foundation. h> @ interface asshape: nsobject-(void) draw; @ end // asshape. M file # import <Foundation/Foundation. h> @ interface asshape: nsobject-(void) draw; @ end
(2) create three sub-classes. The draw method is the same and the content is different. Only one of the following sub-classes is listed. The other ascircle classes and asrectangle classes use their own meditation.
// Astriangle. h file, inherit from asshape class # import "asshape. H "@ interface astriangle: asshape-(void) draw; @ end // similar to the draw of the parent class asshape, but the content output is different # import" astriangle. H "@ implementation astriangle-(void) Draw {nslog (@" triangle: Draw ") ;}@ end
(3) create an asperson class. Use the paint method of this class to treat the above class as a parameter and call their respective draw methods.

// Asperson. h. Use the asshape class because it is used to create the object # import <Foundation/Foundation. h> @ Class asshape; @ interface asperson: nsobject-(void) paint :( asshape *) ashape; @ end // asperson. m. You need to import each header file. Otherwise, their respective draw functions cannot be called at all # import "asperson. H "# import" asshape. H "# import" ascircle. H "# import" asrectangle. H "# import" astriangle. H "@ implementation asperson-(void) paint :( asshape *) ashape {[ashape draw];} @ end
(4) Finally, in Main. create an object in M and use this object to call the painting method. Since the parameter of the painting method is an object of the asshape class, the draw function of the asshape class object should be called, however, because we pass the objects instantiated by the asshape subclass to the paint class as parameters, they call their own draw functions. This should be because the draw is multi-state, so which one to call depends on the parameter you passed.

#import <Foundation/Foundation.h>#import "ASPerson.h"#import "ASShape.h"#import "ASCircle.h"#import "ASRectangle.h"#import "ASTriangle.h"int main(int argc, const char * argv[]){    @autoreleasepool {        ASPerson * person=[[ASPerson alloc]init];        ASCircle * circle=[[ASCircle alloc]init];        ASRectangle * rec=[[ASRectangle alloc]init];        ASTriangle * tri=[[ASTriangle alloc]init];        [person paint:circle];        [person paint:rec];        [person paint:tri];    }    return 0;}

(5) running result

circle:drawrectangle:drawtriangle:draw


Note:

A: Replace the specified parent class with the ID universal pointer, and the running result is the same as OK. The ID universal pointer can point to any object, and calls the method (function) of anyone who points to it ).

@implementation ASPerson-(void)paint:(id)aShape{    [aShape draw];}@end

B: We create a class that does not integrate asshape, which is called asexp. We also define a draw method for it. The procedure is the same as that above. First, an object pair is instantiated, and then the result can be output as follows. That is to say, this polymorphism is not necessarily between the parent class and the subclass.

ASExp * exp=[[ASExp alloc]init];[person paint:exp];

Without the limitations of this parent class and subclass, we may be able to better understand polymorphism. polymorphism is actually a method (function) that writes many in different classes, when a class object is passed, the method (function) of the Class Object is called ).

[OC learning-14] What is polymorphism? View the effect of writing the parent class method in the subclass.

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.