Turing Community first training (inheritance and polymorphism)

Source: Internet
Author: User

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)

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.