OC8_대리 기본 개념

9662 단어 대리
//

//  ProtectedDelegate.h

//  OC8_      

//

//  Created by zhangxueming on 15/6/24.

//  Copyright (c) 2015  zhangxueming. All rights reserved.

//



#import <Foundation/Foundation.h>



@protocol ProtectedDelegate <NSObject>



- (void)bark;



@end







//

//  Person.h

//  OC8_      

//

//  Created by zhangxueming on 15/6/24.

//  Copyright (c) 2015  zhangxueming. All rights reserved.

//



#import <Foundation/Foundation.h>

#import "ProtectedDelegate.h"



@interface Person : NSObject

{

    id <ProtectedDelegate>_delegate;

}//          



@property (retain, nonatomic)id <ProtectedDelegate>delegate;



- (void)go;



@end





//

//  Person.m

//  OC8_      

//

//  Created by zhangxueming on 15/6/24.

//  Copyright (c) 2015  zhangxueming. All rights reserved.

//



#import "Person.h"



@implementation Person



- (void)go

{

    [_delegate bark];

}



@end





//

//  Cat.h

//  OC8_      

//

//  Created by zhangxueming on 15/6/24.

//  Copyright (c) 2015  zhangxueming. All rights reserved.

//



#import <Foundation/Foundation.h>

#import "ProtectedDelegate.h"//     



@interface Cat : NSObject <ProtectedDelegate>//      



@end





//

//  Cat.m

//  OC8_      

//

//  Created by zhangxueming on 15/6/24.

//  Copyright (c) 2015  zhangxueming. All rights reserved.

//



#import "Cat.h"



@implementation Cat

- (void)bark

{

    NSLog(@"Miao miao miao ...");

}

@end









//

//  Dog.h

//  OC8_      

//

//  Created by zhangxueming on 15/6/24.

//  Copyright (c) 2015  zhangxueming. All rights reserved.

//



#import <Foundation/Foundation.h>

#import "ProtectedDelegate.h"



@interface Dog : NSObject <ProtectedDelegate>





@end







//

//  Dog.m

//  OC8_      

//

//  Created by zhangxueming on 15/6/24.

//  Copyright (c) 2015  zhangxueming. All rights reserved.

//



#import "Dog.h"



@implementation Dog



- (void)bark

{

    NSLog(@"Wang wang wang ...");

}



@end







//

//  main.m

//  OC8_      

//

//  Created by zhangxueming on 15/6/24.

//  Copyright (c) 2015  zhangxueming. All rights reserved.

//



#import <Foundation/Foundation.h>

#import "Dog.h"

#import "Cat.h"

#import "Person.h"



int main(int argc, const char * argv[]) {

    @autoreleasepool {

        Person *xiaoXin = [[Person alloc] init];

        Dog *dog = [[Dog alloc] init];

        xiaoXin.delegate = dog;

        [xiaoXin go];

        Cat *cat = [[Cat alloc] init];

        xiaoXin.delegate = cat;

        [xiaoXin go];

    }

    return 0;

}

좋은 웹페이지 즐겨찾기