OC - ProtocolAndDelegate

#import <Foundation/Foundation.h>
#import "Student.h"
#import "Teacher.h"
#import "Landlord.h"
#import "Doctor.h"


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

    @autoreleasepool {
        
// 
    // 
        Landlord *haiYang = [[Landlord alloc] init];
        Teacher *songMeng = [[Teacher alloc] init];
        Student *jinPeng = [[Student alloc] init];
        Doctor *lvSheng = [[Doctor alloc] init];
        
        NSLog(@"---------------------- --------------------------");
        // 
        [haiYang setDelegate:songMeng];
        // 
        [haiYang getMoney];
        // 
        [haiYang checkPublicSomething];
        // 
        [haiYang checkroom];
        
        
        NSLog(@"----------------------- -------------------------");
        // 
        [haiYang setDelegate:jinPeng];
        [haiYang getMoney];
        [haiYang checkPublicSomething];
        [haiYang checkroom];
        
        
         NSLog(@"----------------------- -------------------------");
        // 
        [haiYang setDelegate:lvSheng];
        [haiYang getMoney];
        [haiYang checkPublicSomething];
        [haiYang checkroom];
        
        /* 
         1. 
         2. 
         3. 
         */
        

    }
    return 0;
}

#import <Foundation/Foundation.h>
/*
 1. 
 2. , 
 3. 
 */

@protocol LandlordDelegate <NSObject>

// 
-(void)giveMoney;
// 
-(void)protectPublicSomething;
// 
-(void)cleanHouse;


@end

@interface Landlord : NSObject
{
    NSString *_name;
    NSString *_sex;
    id<LandlordDelegate> _delegate;  // , * 
}

-(void)setDelegate:(id<LandlordDelegate>)delegate;


//   

// 
-( void )getMoney;

// 
-(void)checkPublicSomething;

// 
-(void)checkroom;


@end

#import "Landlord.h"

@implementation Landlord

-(void)setDelegate:(id<LandlordDelegate>)delegate
{
    _delegate = delegate;
}


//   

// 
-( void )getMoney
{
    NSLog(@" ");
    // 
    [_delegate giveMoney];
}

// 
-(void)checkPublicSomething
{
    NSLog(@" ");
     // 
    [_delegate protectPublicSomething];
}

// 
-(void)checkroom;
{
    NSLog(@" ");
    // 
    [_delegate cleanHouse];
}

@end

#import <Foundation/Foundation.h>
// , 
#import "Landlord.h"

@interface Teacher : NSObject <LandlordDelegate>  // 

@end

#import "Teacher.h"

@implementation Teacher

// 

// 
-(void)giveMoney
{
    //
    NSLog(@" ");
}
// 
-(void)protectPublicSomething
{
    NSLog(@" ");
}
// 
-(void)cleanHouse
{
    NSLog(@" ");
}

@end

#import <Foundation/Foundation.h>
#import "Landlord.h"

@interface Student : NSObject <LandlordDelegate>

@end

#import "Student.h"

@implementation Student

// 

// 
-(void)giveMoney
{
    //
    NSLog(@" ");
}
// 
-(void)protectPublicSomething
{
    NSLog(@" ");
}
// 
-(void)cleanHouse
{
    NSLog(@" ");
}

@end

#import <Foundation/Foundation.h>
#import "Landlord.h"

@interface Doctor : NSObject <LandlordDelegate>

@end

#import "Doctor.h"

@implementation Doctor

// 

// 
-(void)giveMoney
{
    //
    NSLog(@" ");
}
// 
-(void)protectPublicSomething
{
    NSLog(@" ");
}
// 
-(void)cleanHouse
{
    NSLog(@" ");
}

@end

#import <Foundation/Foundation.h>

/*
 protocol  
  , ,  .h
  , 
   protocol   +   + <NSObject( )> @end  
 <> 
 */
@protocol rentProtocol <NSObject>


// 
-(void)giveMoney;

// 
-(void)protectPublicSomething;

// 
-(void)cleanHouse;

@end

좋은 웹페이지 즐겨찾기