Object C 分類(category)、擴展(extension)和協(xié)議(protocol)

分類(category)

1.程序員可以將一組相關(guān)的方法放進一個分類,使程序更具可讀性。
2.分類中的方法與類原有的方法并無區(qū)別,其代碼可以訪問包括私有類成員變量在內(nèi)的所有成員變量。
3.若分類聲明了與類中原有方法同名的函數(shù),則分類中的方法會被調(diào)用。因此分類不僅可以增加類的方法,也可以代替原有的方法。

例子:

//Integer.h 文件代碼:
#import <objc/Object.h>

@interface Integer : Object
{
@private int integer;
}

@property (assign, nonatomic) integer;

@end

//Integer.m 文件代碼:
#import "Integer.h"

@implementation Integer

@synthesize integer;

@end
//Arithmetic.h 文件代碼:
#import "Integer.h"

@interface Integer(Arithmetic)
- (id) add: (Integer *) addend;
- (id) sub: (Integer *) subtrahend;
@end

//Arithmetic.m 文件代碼:
#import "Arithmetic.h"

@implementation Integer(Arithmetic)
- (id) add: (Integer *) addend
{
    self.integer = self.integer + addend.integer;
    return self;
}

- (id) sub: (Integer *) subtrahend
{
    self.integer = self.integer - subtrahend.integer;
    return self;
}
@end
//Display.h 文件代碼:
#import "Integer.h"

@interface Integer(Display)
- (id) showstars;
- (id) showint;
@end

//Display.m 文件代碼:
#import "Display.h"

@implementation Integer(Display)
- (id) showstars
{
    int i, x = self.integer;
    for(i=0; i < x; i++)
       printf("*");
    printf("\n");

    return self;
}

- (id) showint
{
    printf("%d\n", self.integer);

    return self;
}
@end
//main.m 文件代碼:
#import "Integer+Arithmetic.h"
#import "Integer+Display.h"

int
main(void)
{
    Integer *num1 = [Integer new], *num2 = [Integer new];
    int x;

    printf("Enter an integer: ");
    scanf("%d", &x);

    num1.integer = x;
    [num1 showstars];

    printf("Enter an integer: ");
    scanf("%d", &x);

    num2.integer = x;
    [num2 showstars];

    [num1 add:num2];
    [num1 showint];

    return 0;
}

擴展(extension)

類擴展一般在實現(xiàn)文件的最上部實現(xiàn),用于擴展類的內(nèi)部實現(xiàn)。
在類擴展中聲明的屬性,編譯器同樣會為其生成相關(guān)的存取方法和實例變量。但是它只能在類的實現(xiàn)內(nèi)部進行訪問。

//類擴展
@interface yourClass () {

     someType someValue;

}

@property someType someProperty;
 -(void)someMethod;

@end

協(xié)議(protocol)

若這個委托對象實現(xiàn)了這個方法,那么類就會在適當?shù)臅r候觸發(fā)自動完成事件,并調(diào)用這個方法用于自動完成功能。

類似多重繼承功能,支持協(xié)議繼承協(xié)議,通過定義一系列方法,然后由遵從協(xié)議的類實現(xiàn)這些方法,協(xié)議方法可以用@optional關(guān)鍵字標記為可選,@required關(guān)鍵字標記為必選

例子

#import <Foundation/Foundation.h>

@protocol PrintProtocolDelegate

@optional
- (void)processCompleted;

@end

@interface PrintClass :NSObject {
   id delegate;
}

- (void) printDetails;
- (void) setDelegate:(id)newDelegate;
@end

@implementation PrintClass
- (void)printDetails {
   NSLog(@"Printing Details");
   [delegate processCompleted];
}

- (void) setDelegate:(id)newDelegate {
   delegate = newDelegate;
}

@end

@interface SampleClass:NSObject<PrintProtocolDelegate>
- (void)startAction;

@end

@implementation SampleClass
- (void)startAction {
   PrintClass *printClass = [[PrintClass alloc]init];
   [printClass setDelegate:self];
   [printClass printDetails];
}

-(void)processCompleted {
   NSLog(@"Printing Process Completed");
}

@end

int main(int argc, const char * argv[]) {
   NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
   SampleClass *sampleClass = [[SampleClass alloc]init];
   [sampleClass startAction];
   [pool drain];
   return 0;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容