IOS学习笔记十二(类别和扩展)
1、类别(category)和扩展
类别:OC动态特征许使用类别添加新的方法,不需要创建子类
扩展:和类别相似,扩展相对于匿名类别
2、使用格式
类别:
@interface 已有类(类别名)
…
@end
@implmentaion 已有类(类别名)
…
@end
扩展:
@imterface 也有类()
{
实例变量
}
//方法
...
@end
3、哪些功能
1)、给一个现成的类增加类别,然后实现其方法
2)、模块化设计
3)、调用私有方法(没有在接口部分定义而是在类实现部分定义的方法相对于私有方法,不许调用),通过定义向前引用,实现对私有方法调用
4、测试类别Demo
NSNumer+fk.h
#ifndef NSNumber_fk_h
#define NSNumber_fk_h
#import <Foundation/Foundation.h>
@interface NSNumber(fk)
-(NSNumber *)add:(double)num2;
-(NSNumber *)sub:(double)num2;
@end
#endif /* NSNumber_fk_h */
NSNumer+fk.m
#import <Foundation/Foundation.h>
#import "NSNumber+fk.h"
@implementation NSNumber(fk)
-(NSNumber *)add:(double)num2
{
return [NSNumber numberWithDouble:([self doubleValue] + num2)];
}
-(NSNumber *)sub:(double)num2
{
return [NSNumber numberWithDouble:([self doubleValue] - num2)];
}
@end
MyApple.h
#import <Foundation/Foundation.h>
#ifndef MyApple_h
#define MyApple_h
@interface MyApple : NSObject
@property (nonatomic, assign) double weight;
@property (nonatomic, copy) NSString *color;
//java方法构造方法一般没有返回,oc这里需要返回id,记住
-(id)initWithColor:(NSString *)color weight:(double)weight;
@end
MyApple.m
#import "MyApple.h"
//#import "MyApple+fk.h"
#import <Foundation/Foundation.h>
@implementation MyApple
@synthesize color = _color;
@synthesize weight = _weight;
-(id)initWithColor:(NSString *)color weight:(double)weight
{
if (self = [super init])
{
self.color = color;
self.weight = weight;
}
return self;
}
-(NSString *) description
{
return [NSString stringWithFormat:@"<MyApple[color=%@, weight=%g]>", self.color, self.weight];
}
-(void)test
{
NSLog(@"this is test method");
}
@end
MyApple+fk.h
#import "MyApple.h"
#ifndef MyApple_fk_h
#define MyApple_fk_h
@interface MyApple(fk)
-(void)test;
@end
#endif /* MyApple_fk_h */
main.h
#import "MyApple.h"
#import "NSNumber+fk.h"
#import "MyApple+fk.h"
int main(int argc, char * argv[]) {
@autoreleasepool {
NSNumber *num = [NSNumber numberWithDouble:3.5];
NSLog(@"3.5 - 2 is %@", [num sub:2]);
NSLog(@"3.5 + 2 is %@", [num add:2]);
MyApple *apple = [[MyApple alloc] initWithColor:@"red" weight:5.6];
NSLog(@"%@", apple);
//No visible @interface for 'MyApple' declares the selector 'test'
[apple test];
}
}
5、运行类别结果
3.5 - 2 is 1.5
3.5 + 2 is 5.5
<MyApple[color=red, weight=5.6]>
this is test method
6、测试扩展的Demo
Car.h
#ifndef Car_h
#define Car_h
#import <Foundation/Foundation.h>
@interface Car : NSObject
@property (nonatomic, copy) NSString *brand;
@property (nonatomic, copy) NSString *model;
-(void)drive;
@end
#endif /* Car_h */
Car+drive.h
#import "Car.h"
#ifndef Car_drive_h
#define Car_drive_h
@interface Car()
@property (nonatomic, copy) NSString *color;
-(void)drive:(NSString *)owner;
@end
#endif /* Car_drive_h */
Car.m
#import <Foundation/Foundation.h>
#import "Car+drive.h"
@implementation Car
@synthesize brand;
@synthesize model;
@synthesize color;
-(void)drive
{
NSLog(@"%@汽车正在路上奔驰",self);
}
-(void)drive:(NSString *)owner
{
NSLog(@"%@正驾驶%@汽车在路上奔驰", owner, self);
}
-(NSString *)description
{
return [NSString stringWithFormat:@"<Car [brand=%@, model=%@, color=%@]>", self.brand, self.model, self.color];
}
@end
main.m
#import "Car+drive.h"
int main(int argc, char * argv[]) {
@autoreleasepool {
Car *car = [Car new];
car.brand = @"奔驰";
car.model = @"S300";
car.color = @"red";
[car drive];
[car drive:@"chenyu"];
}
}
7、运行扩展的结果
<Car [brand=奔驰, model=S300, color=red]>汽车正在路上奔驰
chenyu正驾驶<Car [brand=奔驰, model=S300, color=red]>汽车在路上奔驰
作者:chen.yu
深信服三年半工作经验,目前就职游戏厂商,希望能和大家交流和学习,
微信公众号:编程入门到秃头 或扫描下面二维码
零基础入门进阶人工智能(链接)