Objective-c of language inheritance

Source: Internet
Author: User

• 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

Related Article

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.