Copy的简单实现

COPY

    Woman *w1 = [woman copy];      // w1 woman 的丈夫是同一个人  // woman 的所有拷贝给 w1

    [w1 live];

    

    [w1 release];

    [woman release];

    [man release];

    [boss release];

    

    //输出 woman w1 的地址

    NSLog(@"%p", woman);

    NSLog(@"%p", w1);

    */

   /*

    //初始化对象

    Mother *mother = [[Mother alloc] init];

    Student *stu = [[Student alloc] init];

    Doctor *doctor = [[Doctor alloc] init];

    Teacher *teacher = [[Teacher alloc] init];

    

    //建立代理人关系

    [mother setTeacher:teacher];

    [mother setStudent:stu];

    [mother setDoctor:doctor];

    

    [mother live];

    

    //释放内存

    [stu release];

    [doctor release];

    [teacher release];

    [mother release];

    */

    

   NSString *string = [NSStringstringWithFormat:@"%@%d",@"abc", 123];

   NSLog(@"%@", string);

    

    //不可变字符串对其发送拷贝消息 retain是一样的,只是对原来计时器加一,不会创建新的父本

   NSString *s1 = [string copy];

   NSLog(@"%p", string);

   NSLog(@"%p", s1);

    

    //可变拷贝 mutableCopy   //只能作为方法用,不能放在属性中,属性中只能放retain,copy,assign

   NSMutableString *s2 = [string mutableCopy];

    [s2 insertString:@"iii"atIndex:0];

   NSLog(@"%p", s2);

    

    //打印string的类名

   NSLog(@"%@", [stringclassName]);

   NSLog(@"%@", [s1className]);

   NSLog(@"%@", [s2className]);

    

   NSLog(@"%@", s2);

    

   //规则

    // 1.对不可变字符串发送copy消息,retain一样

    // 2.对可变字符串发送copy消息,会拷贝出一个全新的不可变字符串出来

    // 3.无论对可变或不可变字符串发送mutableCopy的消息,拷贝出来的对象都是全新的不可变的字符串

    

   NSString *s3 = [s2 copy];

   NSLog(@"%p", s3);

   NSLog(@"%@", [s3class]);

    

    //assign:基础变量

    //retain:对象

    //copy:字符串对象

    

Woman.m

#import "Woman.h"


@implementation Woman



- (void)makeUp

{

    NSLog(@"making up");

}


- (void)makeMoney

{

    NSLog(@"making money");

}


- (void)shopping

{

    NSLog(@"happy shopping");

}


- (void)washCloth

{

    NSLog(@"washing cloth");

}


- (void)cook

{

    NSLog(@"cooking");

}


- (void)makeDown

{

    NSLog(@"making down");

}


- (void)live

{

    [selfmakeUp];

    [self.husbandmakeMoney];

    [selfshopping];

   if ([(NSObject *)self.husbandrespondsToSelector:@selector(washCloth)]) {

        //@selector(washCloth)相当于确认了方法名

        [self.husbandwashCloth];

    }else {

        [selfwashCloth];

    }

    

   if ([(NSObject *)self.husbandrespondsToSelector:@selector(cook)]) {

        [self.husbandcook];

    }else {

        [selfcook];

    }

    [selfmakeDown];

}


//copy/init/alloc/开头的方法(我们不允许使用其做字头产生引用计数器加一  在外面手动释放

- (id)copyWithZone:(NSZone *)zone

{

   Woman *woman = [[WomanallocWithZone:zone] init];

    [womansetHusband:self.husband];     //属性一起拷贝

    

   return woman;

}


Woman.h

#import <Foundation/Foundation.h>

#import "MarrayProtocol.h"



@interface Woman :NSObject <NSCopying>


@property (nonatomic,assign) id<MarrayProtocol> husband;


- (void)live;


- (void)makeUp;

- (void)makeMoney;

- (void)shopping;

- (void)washCloth;

- (void)cook;

- (void)makeDown;


@end


MarrayProtocol.h

#import <Foundation/Foundation.h>


@protocol MarrayProtocol


@required      //必须的

- (void)makeMoney;

@optional      //可选的

- (void)washCloth;

- (void)cook;

@end


Man.h

#import <Foundation/Foundation.h>

#import "MarrayProtocol.h"

#import "Assistant.h"


@interface Man : NSObject <MarrayProtocol,Assistant>


- (void)drink;


@end


Man.m

#import "Man.h"


@implementation Man



- (void)makeMoney

{

    NSLog(@"man making money");

}


- (void)washCloth

{

    NSLog(@"man washing cloth");

}


- (void)drink

{

    NSLog(@"happy drinking");

}


- (void)draft

{

    NSLog(@"man draft");

}


- (void)sendMail

{

    NSLog(@"man sendMail");

}





#import "Woman.h"


@implementation Woman



- (void)makeUp

{

    NSLog(@"making up");

}


- (void)makeMoney

{

    NSLog(@"making money");

}


- (void)shopping

{

    NSLog(@"happy shopping");

}


- (void)washCloth

{

    NSLog(@"washing cloth");

}


- (void)cook

{

    NSLog(@"cooking");

}


- (void)makeDown

{

    NSLog(@"making down");

}


- (void)live

{

    [self makeUp];

    [self.husband makeMoney];

    [self shopping];

    if ([(NSObject *)self.husband respondsToSelector:@selector(washCloth)]) {

        //@selector(washCloth)相当于确认了方法名

        [self.husband washCloth];

    } else {

        [self washCloth];

    }

    

    if ([(NSObject *)self.husband respondsToSelector:@selector(cook)]) {

        [self.husband cook];

    } else {

        [self cook];

    }

    [self makeDown];

}


//copy/init/alloc/开头的方法(我们不允许使用其做字头产生引用计数器加一  在外面手动释放

- (id)copyWithZone:(NSZone *)zone

{

    Woman *woman = [[Woman allocWithZone:zone] init];

    [woman setHusband:self.husband];      //属性一起拷贝

    

    return woman;

}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值