OC7_代理的基本概念

时间:2023-03-09 21:20:52
OC7_代理的基本概念
//
// Cat.h
// OC7_代理的基本概念
//
// Created by zhangxueming on 15/6/24.
// Copyright (c) 2015年 zhangxueming. All rights reserved.
// #import <Foundation/Foundation.h> @interface Cat : NSObject - (void)bark; @end //
// Cat.m
// OC7_代理的基本概念
//
// 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
// OC7_代理 的基本概念
//
// Created by zhangxueming on 15/6/24.
// Copyright (c) 2015年 zhangxueming. All rights reserved.
// #import <Foundation/Foundation.h> @interface Dog : NSObject - (void)bark; @end //
// Dog.m
// OC7_代理的基本概念
//
// 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 //
// Person.h
// OC7_代理的基本概念
//
// Created by zhangxueming on 15/6/24.
// Copyright (c) 2015年 zhangxueming. All rights reserved.
// #import <Foundation/Foundation.h> @interface Person : NSObject
{
id _delegate;
} @property (retain,nonatomic)id delegate; - (void)go; @end //
// Person.m
// OC7_代理的基本概念
//
// Created by zhangxueming on 15/6/24.
// Copyright (c) 2015年 zhangxueming. All rights reserved.
// #import "Person.h"
#import "Dog.h"
#import "Cat.h" @implementation Person - (void)go
{
[_delegate bark];
} //- (void)dealloc
//{
// [_delegate release];
// [super dealloc];
//} @end
//
// main.m
// OC7_代理的基本概念
//
// Created by zhangxueming on 15/6/24.
// Copyright (c) 2015年 zhangxueming. All rights reserved.
// #import <Foundation/Foundation.h>
#import "Person.h"
#import "Dog.h"
#import "Cat.h" int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here...
//NSLog(@"Hello, World!");
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 ;
}