1, Basic introduction
Inheritance is an important feature of the class, and his presence makes it unnecessary to write repetitive code, which is highly reusable. Of course, the inheritance in OC and Java is the same, not much difference, here is looking at an example:
First look at the parent class: Car
Car.h
1 #import2 3 @interfacecar:nsobject{4NSString *_brand;5NSString *_color;6 }7 8- (void) Setbrand: (NSString *) Brand;9- (void) SetColor: (NSString *) color;Ten- (void) Brake; One- (void) Quicken; A - @end
Two properties are defined in the car class, and some methods
Car.m
1 #import "Car.h"2 3 @implementationCar4- (void) Setbrand: (NSString *) brand{5_brand =brand;6 }7- (void) SetColor: (NSString *) color{8_color =color;9 }Ten- (void) brake{ OneNSLog (@"Brakes"); A } -- (void) quicken{ -NSLog (@"Acceleration"); the } - @end
Implementation of the method
In a look at the sudden class:
Taxi.h
1 #import "Car.h"2 3 @interfacetaxi:car{4NSString *_company;//Affiliated Companies5 }6 7 //Print Invoice8- (void) Printtick;9 Ten @end
See the taxi class inherits the parent class car, where you need to import the parent class's header file, and then one more property and method in the taxi class
Taxi.m
1 #import "Taxi.h"2 3 @implementationTaxi4 5- (void) printtick{6 [Super brake];7 [self brake];8NSLog (@"%@ Taxi Printed the invoice, the company is:%@, color:%@", _brand,_company,_color);9 }Ten One @end
For the implementation of the method, we see here that the implementation file does not need to import the parent class car header file, because you can think that the Taxi.h header file already contains car's header file. Moreover, the Super keyword can be used here to invoke the parent class method, and here we can also use the Self keyword to invoke, here see actually the two methods of invocation of the effect is the same, when we re-implement the brake method in the sub-class (the rewriting concept in Java), So this time the Super keyword calls the method of the parent class, and self calls the brake method after the rewrite. Similarly, we can also use attributes from the parent class.
Let's look at another subclass:
Truck.h
1 #import "Car.h"2 //Truck class inherit car3 @interfacetruck:car{4 float_maxweight;//Max Cargo Capacity5 }6 7 //methods that override the parent class brake8 //methods to invoke subclasses first9- (void) Brake;Ten One- (void) unload; A - @end
Here you define a brake method, which overrides the brake method in the parent class.
Truck.m
1Import"Truck.h"2 3 @implementationTruck4 5- (void) brake{6 [Super brake];7NSLog (@"brake methods in the truck class");8 }9 Ten- (void) unload{ One[Super brake];//calling a method of the parent class A[Self brake];//It 's also possible. -NSLog (@"%@ 's truck was unloaded, cargo capacity:%.2f, color of the car:%@", _brand,_maxweight,_color); - } the - @end
As you can see here, we will invoke the brake method of the parent class in the brake method, and then implement our own logic code.
2. Other knowledge points of inheritance
1. Inheritance of professional terminology
Parent class \ Super class Superclass
Sub-class Subclass\subclasses
2. Details of the inheritance
Single inheritance
Subclasses and parent classes cannot have the same member variable
Override of Method
3. Super keyword
Calling the parent class's object methods and class methods, respectively
4. Benefits of Inheritance
Do not change the original model based on the method of extension charge
Establish a connection between classes and classes
The public code was extracted
Cons: Strong coupling
5. Application of Inheritance
All of its properties are what you want, generally inherited
Some of its properties are what you want, you can extract another parent class
6. Access procedures for subclass methods and properties:
If the subclass does not, it accesses the parent class's
The parent class is inherited or can be used as usual.
Static methods of the parent class
Draw an inheritance structure diagram from a subclass to a parent class
NSObject: The final parent class of all OC classes, including some common methods, such as +new
3, example design two classes of bird, Dog
1 //Bird's statement2 @interfaceBird:nsobject3 {4 @public5 intweight;6 }7- (void) eat;8 @end9 //definition of birdTen @implementationBird One- (void) Eat { ANSLog (@"eat-weight:%d", weight); - } - @end the //the statement of dog - @interfaceDog:nsobject - { - @public + intweight; - } +- (void) eat; A @end at //definition of dog - @implementationDog -- (void) Eat { -NSLog (@"eat-weight:%d", weight); - } - @end in have the same properties and behavior, extract a parent class animal (extract the weight attribute first, then extract the Eat method) - //Animal's statement to @interfaceAnimal:nsobject + { - @public the intweight; * } $- (void) eat;Panax Notoginseng @end - //definition of animal the @implementationAnimal +- (void) Eat { ANSLog (@"eat-weight:%d", weight); the } + @end - subclass on the basis of the parent class to expand the properties and methods $ //Bird's statement $ @interfaceBird:animal - { - @public the intheight; - }Wuyi- (void) Fly; the @end - Wu //definition of bird - @implementationBird About- (void) Fly { $NSLog (@"Flying Fly -height:%d", height); - } - @end - A //the statement of dog + @interfaceDog:animal the { - @public $ intSpeed ; the } the- (void) run; the @end the //definition of dog - @implementationDog in- (void) Run { theNSLog (@"Run Run -height:%d", speed); the } About @end
The three major characteristics of OC--inheritance