Inheritance: Inheritance is the technique of building a new class using the definition of an existing class, and the new class inherits the old class, which can be used to get some of the data from the old class. Examples include inheritance of instance variables and inheritance of methods.
Old Class AAA
@interfance Aaa:nsobject
{
int Val;
}
-(int) Intvar;
@end
@implementation Aaa
-(int) Intvar
{
var=1000;
return var;
}
@end
New class ABB
#import "Aaa.h"
@interface ABB:AAA
-(void) print;
@end
Abb.m
-(void) print
{
NSLog (@ "%i", Var);
}
Main.m
ABB *a=[[abb Alloc]init];
NSLog (@ "%i", [a Intvar]);//abb inherits the AAA method
Polymorphism: Since the methods and variables between classes and classes can be inherited, whether the old class method can be overridden in the new class. Of course, this is polymorphic (with C + + basic Children's shoes will be able to quickly accept and understand)
For example, the above example
In ABB.M
-(int) Intvar
{
NSLog (@ "although in ABB, but I rewrote the method in AAA");
}
We can re-write the polymorphic drops-(int) Intvar; this method
Aaa *a=[[aaa Alloc]init];
ABB *b=[[abb Alloc]init];
[a print];
[B print];
Turing Community first training (inheritance and polymorphism)