oc inheritance (the three major characteristics of object-oriented encapsulation, inheritance, polymorphism)
Original link: http://www.jianshu.com/p/568077956a9a
The copyright belongs to the author, please contact the author for authorization, and mark "author of Jianshu".
1. The pointer of the parent class can point to the object of the subclass
2. When calling the method, the pointer does not look at the object.
//Parent class, there is a method jump //Father.h #import <Foundation/Foundation.h> @interface Father : NSObject - (void)jump; @end //Father.m #import "Father.h" @implementation Father -(void)jump { NSLog(@"Father can jump 1.2m."); } @end //Subclass, override the jump method //Son.h #import"Father.h" @interface Son : Father //Override, no need to declare @end //Son.m #import "Son.h" @implementation Son -(void)jump { NSLog(@"Son can jump 1.8m."); } @end //main.m #import <Foundation/Foundation.h> #import "Father.h" #import "Son.h" int main(int argc,const char* argv[]) { @autoreleasepool { Son* son = [[Son alloc]init]; Father* father = son;//The pointer of the parent class points to the object of the child class [father jump];//Call the jump of the parent class or the jump of the subclass? // still call the method of the subclass //Do not look at the pointer when calling the method, look at the object //The address of the object calls the method of the object } return 0; }Such methods are called virtual methods, and can describe different things being triggered by the same event, producing different responses (results). Write a program to beat a small animal below.
//The parent class, Animal, has a beBeaten method that describes the response when being hit //Animal.h #import <Foundation/Foundation.h> @interface Animal : NSObject -(void)beBeBeaten;//Response when being beaten @end //Animal.m #import "Animal.h" @implementation Animal -(void)beBeaten { return ;//Virtual method, you can do nothing, every subclass will override this method } @end //Cat.h #import "Animal.h" @interface Cat : Animal @end //Cat.m #import "Cat.h" @implementation Cat -(void)beBeaten { NSLog(@"Bark and jump to high!"); } @end //Dog.h #import "Animal.h" @interface Dog : Animal @end //Dog.m #import "Dog.h" @implementation Dog -(void)beBeaten { NSLog(@"Give a hard bit!"); } @end //Frog.h #import "Animal.h" @interface Frog : Animal @end //Frog.m #import "Frog.h" @implementation Frog -(void)beBeaten { NSLog(@"Do nothing!"); } @end //Human class, there is a method beat #import <Foundation/Fountion.h> #import "Animal.h" @interface Human : NSObject -(void)beatAnimal:(Animal*)animal;//The pointer of the parent class can point to the address of any subclass, otherwise, to beat different animals, you need to create different animal objects @end //Human.m #import "Human.h" @implementation Human //Only need to write one method, although the animals are different, this is the advantage that the parent class can point to the child class -(void)beatAnimal:(Animal*)animal { NSLog(@"Human beat the %@",[animal class]); [animal beBeaten]; } @end //main.m #import <Foundation/Foundation.h> #import "Dog.h" #import "Cat.h" #import "Frog.h" #import "Human.h" int main(int argc,const char* argv[]) { @autoreleasepool { Frog* frog = [[Frog alloc]init]; Dog* dog = [[Dog alloc]init]; Cat* cat = [[Cat alloc]init]; Human* Linda = [[Human alloc]init]; //who is Linda? [Linda beatAnimal:frog];//Different things are triggered by the same event, resulting in different responses [Linda beatAnimal:cat]; [Linda beatAnimal:dog]; } return 0; }Notice:
polymorphism
Polymorphism is based on inheritance, the parent class pointer points to the child class object
Person *p = [Man new];
same
NSObject *n = [Man new];
Notice:
1. If the parent class type is used in the function and method parameters, the parent class and subclass objects can be passed in.
2. The parent class type object cannot directly call the subclass-specific (not overridden) methods, and must be cast to the subclass type variable before calling.
The compilation will report an error, and the operation is still the same, but it is not recommended to write. (because oc is weakly typed)
If the above two sentences call [n eat], there will only be a warning, and the eat method of executing man can still be dynamically checked.