//
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)