IOS學習筆記十二(類別和擴展)

1、類別(category)和擴展

類別:OC動態(tài)特征許使用類別添加新的方法,不需要創(chuàng)建子類

擴展:和類別相似,擴展相對于匿名類別

2、使用格式
類別:

    @interface 已有類(類別名)
     
    …
     
    @end

    @implmentaion 已有類(類別名)
     
    …
     
    @end

擴展:

    @imterface 也有類()
    {
        實例變量
    }
    //方法
     
    ...
     
    @end

 
3、哪些功能

1)、給一個現(xiàn)成的類增加類別,然后實現(xiàn)其方法

2)、模塊化設(shè)計

3)、調(diào)用私有方法(沒有在接口部分定義而是在類實現(xiàn)部分定義的方法相對于私有方法,不許調(diào)用),通過定義向前引用,實現(xiàn)對私有方法調(diào)用


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方法構(gòu)造方法一般沒有返回,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、運行類別結(jié)果

    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、運行擴展的結(jié)果

    <Car [brand=奔馳, model=S300, color=red]>汽車正在路上奔馳
    chenyu正駕駛<Car [brand=奔馳, model=S300, color=red]>汽車在路上奔馳



 

 

 
 
作者:chen.yu
深信服三年半工作經(jīng)驗,目前就職游戲廠商,希望能和大家交流和學習,
微信公眾號:編程入門到禿頭 或掃描下面二維碼
零基礎(chǔ)入門進階人工智能(鏈接)