• Inheritance is one of the important concepts of object-oriented, and subclasses can inherit certain methods and member variables of the parent class. Member variables that have a scope qualifier of private are not inheritable. The child can also override the method of the parent class.
• Inheritance is a single inheritance, and multiple inheritance introduces the protocol
• The subclass defines the same member variable as the parent class, which masks the member variables of the parent class
• To understand inheritance, let's look at a scenario where an object-oriented programmer, Xiao Zhao, needs to describe and process personal information during programming, so he defines the class person.
@interface Person:nsobject {nsstring* name; int Age ; NSDate birthDate;} -(nsstring*) getInfo; @end
A week later, Xiao Zhao met a new demand, need to describe and deal with student information, so he also defined a new class student.
@interface Student:nsobject {nsstring* name; int Age ; NSDate birthDate; NSString* School;} -(nsstring*) getInfo; @end
The structure of the student and person two classes is too close, and the latter only has one more attribute school than the former , but repeats all the other content. Objective-c provides a mechanism for solving similar problems, which is the inheritance of classes.
@interface Student:person {nsstring* School;}
a subclass cannot inherit a member variable with a scope qualifier of @private in the parent class . Subclasses can override the parent class's methods and name member variables with the same name as the parent class . The following is an example of a rectangular class and a square class that describes how to override the problem.
#import <Foundation/NSObject.h>@interface Rectangle:nsobject {intwidth;intheight;}-(rectangle*) Initwithwidth: (int) W Height: (int) H;-(void) SetWidth: (int) W;-(void) SetHeight: (int) H;-(void) SetWidth: (int) W Height: (int) H;-(int) width;-(int) height;-(void) print; @end
#import rectangle.h" @implementation Rectangle -(rectangle*) Initwithwidth: (int ) W Height: ( int ) h {self = [super Init]; if return self;} -(void ) SetWidth: (int ) w {width = W;} -(void ) SetHeight: (int ) H {height = H;} ... ...
-(void) SetWidth: (int) w height: (int) H {
width = w;
Height = h;
}
-(int) Width {
return width;
}
-(int) Height {
return height;
}
-(void) Print {
printf ("width =%i, height =%i", Width,
height);
}
@end
" Rectangle.h " @interface Square:rectangle
-(square*) Initwithsize: (int) s; -(void) SetSize: (int) s; -(int) size; @end
#import"Square.h"@implementation Square-(square*) Initwithsize: (int) s { self=[Super init];if(self) {[self setsize:s];}returnSelf ;}-(void) SetSize: (int) s {width=S;height=s;} ... ...-(int) Size {returnwidth;}-(void) SetWidth: (int) W {[Self setsize:w];}-(void) SetHeight: (int) h {[Self setsize:h];} @end
#import <Foundation/Foundation.h>#import"Square.h"#import"Rectangle.h"intMain (intargcConst Char*argv[]) {Rectangle*rec = [[Rectangle alloc] Initwithwidth:TenHeight -]; Square*SQ = [[Square alloc] Initwithsize: the]; NSLog (@"Rectangle:" ); [Rec print]; NSLog (@"Square:" ); [sq Print]; [Sq SetWidth: -]; NSLog (@"Square after change:" ); [sq Print]; [Rec release]; [sq release];return 0;}
Run Results
Ten - the
Objective-c of language inheritance