Objective-c Learning article 04-polymorphism

Source: Internet
Author: User


Polymorphic

  The concept of polymorphism

There is an example of this. I told my colleague that I was thirsty in the morning. Result: A colleague took my cup to fetch me a glass of water. B My colleague took a disposable paper cup on the water dispenser and brought me a glass of water. c My colleague gave me a bottle of the drink he had just bought in the morning. Colleagues get the same request, but three people have their own solution, which is polymorphism.

In object-oriented programming theory, polymorphism (polymer phism) refers to the effect of different execution when the same operation is applied to instances of different classes. That is, when objects of different classes receive the same message, different results can be obtained.

We know that OC is a dynamic language, and when using polymorphism, the system will dynamically detect to invoke the real object method. In the context of inheritance, polymorphism is reflected in code as a parent class pointer to a subclass object.

Realize:

First create a animal class to inherit from NSObject

Animal.h
@interface Animal:nsobject
// declaring how animals eat -(void) eat; @end

animal.m
@implementation
-(void

NSLog (@ " animals eat "
@end

Then create a cat class and a dog class to rewrite your own unique method.

Cat class

Cat.h

// cat inherits from Animal class
@interface Cat:animal
@end
Cat.m
@implementation

Rewriting the cat's unique approach
-(void
NSLog (@ " cat eats fish "

@end

Dog class

Dog.h

Dog inherits from the animal class and has all the properties and methods of animal
@interface Dog:animal
@end
Dog.m
@implementation Dog

Rewrite the dog's unique approach
-(void
NSLog (@ " dog Eats bones "
@end

Go back to main to implement:

// Create a Animal object *animal = [[Animal alloc] init];             // Multi-State implementations:     //1. parent class pointer to child class object = [[Cat alloc] init];          // Is the method called at this time the animal eats the thing? or the cat eats ?     [Animal eat];
//2. = [[Dog alloc] init]; [Animal eat];

Print:

2015-11-27 17:31:38.975 duotai[1597:251835] cat eats fish

2015-11-27 17:31:38.975 duotai[1597:251835] dog eats bones

Let's look at two more lines of code:

Dog *dog = [[Animal alloc] init]; Animal is a dog? Is this logic correct??

NSString *string = [[Dog alloc] init]; Dog is a string??

By the above found:

  OC language is a weak grammar of the language, compile time and will not error, the system will only issue a warning. So this requires us in the actual development process must be in accordance with the established norms to write code.

  The limitations of polymorphism

  A parent pointer cannot call a method unique to a subclass , although it can be called successfully, but it is not canonical to write

    Animal *animal2 = [[Cat alloc] init];
[Animal2 eat];
// [Animal2 Catchmouth];

Benefits of polymorphism:

Girls generally prefer to keep pets, now create a girl class, let her have the way to keep pets

Girl.h

@class Animal;//In Girl.h, introduce the Animal header file with @class, and tell the compiler that a class @interface Girl:nsobject
-(void) Feedanimal: (Animal *) Animal; @end

girl.m

#import " Animal.h "//#import introduction of animal header files in girl.m @implementation Girl
-(void) Feedanimal: (Animal *) Animal { NSLog (@ " girl keeping pet%@" , animal);}
@end

Main.m

 //  Create a Girl object   Girl  *girl = [[Girl alloc] init];  //  Ex-boyfriend gave it a cat cat, she needs to feed   cat  *cat = [[Cat alloc] init];  //  Girl object calls the method of raising animals, passing the cat in   [girl Feedanimal:cat];  //  Now, the ex-boyfriend dumped, met the new boyfriend, the new boyfriend sent a dog enough to give her,    She also needs to raise   Dog  *d = [[Dog alloc] init];  //   [girl Feedanimal:d];  

//Print results

2015-11-27 22:02:22.887 duotai[1812:401072] girl keeping pets <Cat: 0x100206410>

2015-11-27 22:02:22.887 duotai[1812:401072] girl keeping pets <dog: 0x1003006c0>

  The advantages of polymorphism

Cats and dogs are inherited from the animal class, where we can use polymorphism to simplify the code, only need to write the parameters of the function animal * type, then the dog and cat types of objects can be passed in, in the call to change the parameters directly.

  Polymorphism is an important feature of object-oriented programming, which greatly enhances the flexibility and extensibility of software.

Objective-c Learning article 04-polymorphism

Related Article

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.