IOS學(xué)習(xí)筆記十二(類(lèi)別和擴(kuò)展)
1、類(lèi)別(category)和擴(kuò)展
類(lèi)別:OC動(dòng)態(tài)特征許使用類(lèi)別添加新的方法,不需要?jiǎng)?chuàng)建子類(lèi)
擴(kuò)展:和類(lèi)別相似,擴(kuò)展相對(duì)于匿名類(lèi)別
2、使用格式
類(lèi)別:
@interface 已有類(lèi)(類(lèi)別名)
…
@end
@implmentaion 已有類(lèi)(類(lèi)別名)
…
@end
擴(kuò)展:
@imterface 也有類(lèi)()
{
實(shí)例變量
}
//方法
...
@end
3、哪些功能
1)、給一個(gè)現(xiàn)成的類(lèi)增加類(lèi)別,然后實(shí)現(xiàn)其方法
2)、模塊化設(shè)計(jì)
3)、調(diào)用私有方法(沒(méi)有在接口部分定義而是在類(lèi)實(shí)現(xiàn)部分定義的方法相對(duì)于私有方法,不許調(diào)用),通過(guò)定義向前引用,實(shí)現(xiàn)對(duì)私有方法調(diào)用
4、測(cè)試類(lèi)別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方法構(gòu)造方法一般沒(méi)有返回,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、運(yùn)行類(lèi)別結(jié)果
3.5 - 2 is 1.5
3.5 + 2 is 5.5
<MyApple[color=red, weight=5.6]>
this is test method
6、測(cè)試擴(kuò)展的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(@"%@汽車(chē)正在路上奔馳",self);
}
-(void)drive:(NSString *)owner
{
NSLog(@"%@正駕駛%@汽車(chē)在路上奔馳", 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、運(yùn)行擴(kuò)展的結(jié)果
<Car [brand=奔馳, model=S300, color=red]>汽車(chē)正在路上奔馳
chenyu正駕駛<Car [brand=奔馳, model=S300, color=red]>汽車(chē)在路上奔馳
作者:chen.yu
深信服三年半工作經(jīng)驗(yàn),目前就職游戲廠商,希望能和大家交流和學(xué)習(xí),
微信公眾號(hào):編程入門(mén)到禿頭 或掃描下面二維碼
零基礎(chǔ)入門(mén)進(jìn)階人工智能(鏈接)