Small white Study Development (IOS) OC_ using inheritance to augment classes (2015-08-07)

Source: Internet
Author: User

//

Main.m

// use inheritance to enrich classes

//

Created by admin on 15/8/12.

Copyright (c) years admin. All rights reserved.

//

/*

When there are too many methods in a class to be very complex, to be clear, we can create a category of classes to augmentthis class

in other object-oriented languages, an inheritance relationship class is used to augment a class, but the biggest disadvantage is that the coupling is too high

OC Use (category) to augment a class

Note:1. As long as the member variables generated in the original class (except for private variables) are directly accessible in the taxonomy

2. If the implementation class is defined in the classification with the same name as the original class method, then the method in the original class is overwritten (in real development, it is best not to appear method overrides)

3. You cannot define member variables in the taxonomy, only add methods

*/

#import <Foundation/Foundation.h>

#import "Person.h"

#import "Person+blackperson.h"


int Main (int argc,const Char * argv[]) {

@autoreleasepool {

person *p = [[personalloc]init];

[P eat]; // here Call the eat method, run visible is print " negro eats ", so the method is overwritten

}

return 0;

}

//

Person.h

the specific syntax of OC

//

Created by admin on 15/8/12.

Copyright (c) years admin. All rights reserved.

//


#import <Foundation/Foundation.h>


@interface Person:NSObject

{

int _age;

}

@property (nonatomic,assign)int age1;

@property (nonatomic,strong)nsstring *name;


// human basic methods

-(void) eat;

-(void) drink;

-(void) smile;


@end

//

Person.m

the specific syntax of OC

//

Created by admin on 15/8/12.

Copyright (c) years admin. All rights reserved.

//


#import "Person.h"


@implementation Person


-(void) eat

{

NSLog(@ " human eating ");

}

-(void) drink

{

NSLog(@ " human drink ");

}

-(void) smile

{

NSLog (@ " human Smile ");

}


@end


//

Person+blackperson.h

the specific syntax of OC

//

Created by admin on 15/8/12.

Copyright (c) years admin. All rights reserved.

//


#import "Person.h"


@interface Person (Blackperson)


-(void) popping;

-(void) eat1; // This overrides the Eat method in the person class


@end


//

Person+blackperson.m

the specific syntax of OC

//

Created by admin on 15/8/12.

Copyright (c) years admin. All rights reserved.

//


#import "Person+blackperson.h"


@implementation Person (Blackperson)


-(void) popping

{

[selfeat];

_age =; the member variables in the original class can be called directly in the //category

NSLog(@ " black people Can Dance ");

}


-(void) EAT1

{

NSLog(@ " black eats ");

}




@end




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Small white Study Development (IOS) OC_ using inheritance to augment classes (2015-08-07)

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.