OC Object-oriented-polymorphic

Source: Internet
Author: User

OC Object-oriented-polymorphic

First, the basic concept

  

 1> polymorphism in the code of the embodiment, that is, a variety of forms, no inheritance there is no polymorphism

2> when using polymorphism, dynamic detection is performed to invoke the real object method

The embodiment of 3> in code is that the parent pointer points to the child class object

Declaration of the Animal class

#import <Foundation/Foundation>// Declare a animal class that inherits from NSObject@interface Animal:nsobject // declares a Eat object method in the Animal class -(void) eat; @end

Implementation of the Animal class

#import " Animal.h " /* implementation of the *eat method */@implementation Animal
-(void) eat{ NSLog (@ "Animal Eats");} @end

Dog inherits from Animal class

#import " Animal.h " /* The *dog class inherits from the animal class and has all the properties and methods of the animal class . */ @interface Dog:animal // The eat method that is already in the parent class is declared in the subclass, which is called the rewrite of the method -(void) eat; @end

The implementation of the Dog class

#import " Dog.h " @implementation Dog /* * Rewrite eat */-(void)    eat{NSLog (@ " dog eats ");} @end

Test program

#import<Foundation/Foundation>#import "Animal.h"#import "Dog.h"intMain () {//a dog type pointer to an object of type dogDog * dog=[[Dog alloc] init]; .//The dog class object calls the object method[Dog Eat]; // polymorphic//parent class pointer to child class objectAnimal * a=[[Dog alloc] init]; //This is called the Eat method in the dog class.[a eat]; //A pointer to the animal type points to an object of type animalAnimal * a1=[[Animal alloc] init];        [A1 eat]; //the following are the manifestation of polymorphismNSObject *n=[[Dog alloc] init]; //[n Eat]//You cannot call thisNSObject * n1=[[Animal alloc] init]; return 0;}

Second, the use of attention

Code Analysis

Dog *dog=[[animal alloc] init]; Is the animal a dog? Is the semantics correct?

NSString *str=[dog New]; is a dog a string? right?

OC language is a weak language, compile time and will not error, so this requires us in the actual development process must follow the established specifications to write code, do not appear that the dog is a string problem.

Benefits of Titus:

A new function is needed to feed the dog.

void Feed (Dog *d) {

[D eat];

}

If you need to feed the cat at this time, you should rewrite a new function

void Feed (Cat *c) {

[C Eat];

}

And the dog and the cat actually inherit from the animal this class, here can use polymorphic to simplify the code, here only need to write the function parameter is animal * Type, then the dog and the cat type object can all pass in, the call time directly changes the parameter to be possible.

Limitations of polymorphism:

A pointer variable of a parent class type cannot directly invoke a method that is unique to a subclass.

Non-Recommended Practice:

Animal *a=[[dog alloc] init];

[A run];//does not have a run method in the animal class, which invokes the method of the dog object.

Workaround: You can cast a to a variable of type dog*, as follows:

Dog *d= (dog *) a;//uses casts, where a and D point to the same dog object

Iii. Summary of multi-state use

1> There's no inheritance, it's polymorphic.

The embodiment of the 2> code: a pointer to the parent class type points to the subclass object

3> Benefits: If you are using a parent class type in a function method parameter, you can pass in the parent class and the subclass object without having to define multiple functions to match the corresponding class.

4> Limitations: Variables of the parent class type do not give you a direct call to a subclass-specific method, and must be cast to a subclass-specific method if it must be called

Iv. String Complement content

The @ "123" string is also an object, belonging to the NSString class, following some code descriptions for the string object:

#import<Foundation/Foundation>intMain () {//The simplest way to create a stringNSString *str=@"Zhang San"; Char*name="Zhang San"; //Print a stringNSLog (@"%s", name); NSLog (@"%@ is reading a book .", str); intA=5; intb=Ten; //another way to create a stringNSString *str=[nsstring stringWithFormat:@"I've been knocking for%d hours, ready to learn%d hours.", A, b]; NSLog (@"%@", str); return 0;}

The length method of the string object: Computes the word count of the string, rather than the number of characters, as in the case of the strlen function. For example, the result of "Ha ha123" length is 6, which returns the unsigned long type, and the Strlen function results in 8, Because a Chinese character accounts for 3 bytes.

Tip: Word count also includes spaces

Purpose: Assign a program constant to the name of the symbol and place it at the beginning of the program, but at the back of the #import

Note : It is best to capitalize the defined constants

OC Object-oriented-polymorphic

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.