Objective-c----Inheritance

Source: Internet
Author: User

First, the concept:

Object-oriented provides inheritance syntax that can greatly simplify code. The common methods and instance variables are written in the parent class, and the subclass words need to write their own unique instance variables and methods.

Inheritance guarantees both the integrity of the class and the simplification of the code.

Second, the characteristics of inheritance:

Only single inheritance is allowed in OC.
A class without a parent class becomes the root class, and the root class in OC is NSObject (ancestor).
Inherited content: All instance variables and methods.
If the subclass is not satisfied with the implementation of the parent class method, you can override the (overwrite) method of the parent class.

Three, method scheduling mechanism:

When the code sends a message, the Objective-c method scheduling mechanism will search for the appropriate method in the current class. If the corresponding method cannot be found in the class file of the object that receives the message, it will look in the superclass of the object.

Each method call obtains a hidden argument named Self, which is a pointer to the object that receives the message. These methods use the self parameter to find the instance variables they need.

Super, which is neither a parameter nor an instance variable, but is sent by Objective-c to the superclass class. If no change message is defined in the superclass, OBJECTIVE-C will continue to look in the inheritance chain as usual. Always find the root class.

Four, under what circumstances to use composite? What is the case with inheritance?

The relationships established between inherited classes are "is a" (one). The triangle is a shape, V6 is an engine, and Michelin is the name of a tyre. If you can say "X is a Y", then you can use inheritance.
On the other hand, the relationship established between the composite classes is "has a" (there is one). The shape has a fill color, the car has an engine and four tires. Unlike inheritance, a car is not an engine, nor a tyre. If you can say "X has a Y", then you can use compositing.

Five, the direct example code
#import <Foundation/Foundation.h>#import "Student.h"intMainintargcConst Char* argv[]) {//A class can have more than one subclass, a class may have only one direct parent (the concept of single inheritance), but it can have more than one superclass. Student *student = [[Student alloc] initwithname:@"Wang"gender:@"NV"Age to] ;    [Student eat];    [Student sleep]; Student *otherstudent = [[Student alloc] initwithname:@"Zhen"gender:@"Nan"Age ANumber11011schoolname:@"Hafu"] ; [Otherstudent Study];return 0;}

Student.h file:

////Student.h////Created by on 15/4/1.//Copyright (c) 2015. All rights reserved.//#import "Person.h" //Defines a subclass that defines its own characteristics and behavior based on the characteristics (instance variables) and behavior (methods) inherited by the parent class @interface Student :   person{int_number;NSString*_schoolname;} - (void) Setnumber: (int) number;-(void) Setshoolname: (NSString*) Schoolname;-(int) number;-(NSString*) Schoolname;-(Instancetype) Initwithname: (NSString*) name Gender: (NSString*) Gender Age: (int) Age Number: (int) Number Schoolname: (NSString*) Schoolname;-(void) study;@end //Student:person

STUDENT.M file:

////STUDENT.M////Created by on 15/4/1.//Copyright (c) 2015. All rights reserved.//#import "Student.h"  @implementation Student - (void) Setnumber: (int) Number {_number = number;}//Setnumber- (void) Setshoolname: (NSString*) Schoolname {_schoolname = Schoolname;}//Setschoolname- (int) Number {return_number;}// number- (NSString*) Schoolname {return_schoolname;}//Schoolname-(Instancetype) Initwithname: (NSString*) name Gender: (NSString*) Gender Age: (int) Age Number: (int) Number Schoolname: (NSString*) Schoolname {if( Self= [SuperInitwithname:name Gender:gender Age:age]) {_number = n        umber;    _schoolname = Schoolname; }return  Self;}//Initwithname- (void) Study {NSLog( @"%@ in%@ School, study number:%d!", _name, _schoolname, _number);}//Study- (void) Eat {[SuperEat];NSLog( @"Hexibeifeng") ;}//Eat@end //Student:person

Person.h file:

////Person.h////Created by on 15/4/1.//Copyright (c) 2015. All rights reserved.//#import <Foundation/Foundation.h>  @interface  Person: nsobject {NSString*_name;NSString*_gender;int_age;}//setter Setup- (void) SetName: (NSString*) name;-(void) Setgender: (NSString*) gender;-(void) Setage: (int) Age;//getter Accessors- (NSString*) name;-(NSString*) gender;-(int) Age;//Initialize-(Instancetype) Initwithname: (NSString*) name Gender: (NSString*) Gender Age: (int) Age;-(void) eat;//Eat- (void) sleep;//Sleeping@end  // person

PERSON.M file:

////PERSON.M////Created by on 15/4/1.//Copyright (c) 2015. All rights reserved.//#import "Person.h"  @implementation   person- (void) SetName: (NSString*) name {_name = name;}//SetName- (void) Setgender: (NSString*) Gender {_gender = gender;}//Setgender- (void) Setage: (intAge {_age = age;}//Setage- (NSString*) name {return_name;}//GetName- (NSString*) Gender {return_gender;}//Getgender- (int) Age {return_age;}//Getage-(Instancetype) Initwithname: (NSString*) name Gender: (NSString*) Gender Age: (int) Age {if( Self= [SuperInit]) {_name = name;        _gender = gender;    _age = age; }return  Self;}//Initwithname- (void) Eat {NSLog( @"%@ in the restaurant!" ", _name);}//Eat- (void) Sleep {NSLog( @"%@ is sleeping!" ", _name);}//Sleep@end  // person

Objective-c----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.