Object C 分類(lèi)(category)、擴(kuò)展(extension)和協(xié)議(protocol)

分類(lèi)(category)

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

例子:

//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;
}

擴(kuò)展(extension)

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

//類(lèi)擴(kuò)展
@interface yourClass () {

     someType someValue;

}

@property someType someProperty;
 -(void)someMethod;

@end

協(xié)議(protocol)

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

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

例子

#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)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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