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;

}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章