2013-8-5学习练习 协议

来源:互联网 发布:tcpip协议 默认端口号 编辑:程序博客网 时间:2024/05/21 00:00
1浅拷贝和深拷贝的区别和相同点;

浅拷贝就比如像引用类型,而深拷贝就比如值类型。

浅拷贝是指源对象与拷贝对象共用一份实体,仅仅是引用的变量不同(名称不同)。对其中任何一个对象的改动都会影响另外一个对象。

深拷贝是指源对象与拷贝对象互相独立,其中任何一个对象的改动都不会对另外一个对象造成影响。


2用什么方法可以生成一个完全相同的可变数组(1)给你一个不可变数组(2)给你一个可变对象;
NSMultableArray+test.h:

#import <Foundation/Foundation.h>

@interface NSMutableArray (test)

+(id)copyWithMutableArray:(NSMutableArray *)mulArr;

+(id)copywithArray:(NSArray *)Arr;

@end

NSMultableArray+test.m:

#import "NSMultableArray+test.h"

@implementation NSMutableArray (test)

+(id)copyWithMutableArray:(NSMutableArray *)mulArr

{

    NSMutableArray *mulArray = [NSMutableArray arrayWithArray:[mulArr copy]];

    return mulArray;

}

+(id)copywithArray:(NSArray *)Arr

{

    NSArray *Array = [NSArray arrayWithArray:[Arr copy]];

    return Array;

}

@end

main:

#import <Foundation/Foundation.h>

#import "NSMultableArray+test.h"

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

{

    @autoreleasepool {

        NSMutableArray *mutableArray = [[[NSMutableArray alloc] init] autorelease];

        NSMutableArray *mularr = [[NSMutableArray alloc] initWithObjects:@"1",@"2",@"3",nil];

        mutableArray = [NSMutableArray copyWithMutableArray:mularr];

        NSLog(@"%@",mutableArray);

        NSArray *array = [[NSArray alloc] initWithObjects:@"4",@"5",@"6",nil];

        mutableArray = [NSMutableArray copywithArray:array];

        NSLog(@"%@",mutableArray);

    }

    return 0;

}

结果:

2013-08-05 20:35:41.152 作业2[3062:303] (

    1,

    2,

    3

)

2013-08-05 20:35:41.154 作业2[3062:303] (

    4,

    5,

    6

)


3老师发试卷,学生做题目,做完题目报告,老师收试卷(用协议委托写这个过程);
protocolText.h:

#import <Foundation/Foundation.h>

@protocol protocolText <NSObject>

@optional

-(void)Excise;//做题目

-(void)ReceivePager;//收试卷

@end

Student.h:

#import <Foundation/Foundation.h>

#import "protocolText.h"

@interface Student : NSObject<protocolText>

@property(nonatomic,retain) id<protocolText> stuDelegate;

-(void)receive;//叫老师收试卷

-(void)Excise; //学生做试卷

@end

Student.m:

#import "Student.h"

@implementation Student

-(void)receive

{

    if ([_stuDelegate respondsToSelector:@selector(ReceivePager)]) {

        [_stuDelegate ReceivePager];

    }

}

-(void)Excise

{

    NSLog(@"学生做试卷");

}

- (void)dealloc

{

    [_stuDelegate release];

    [super dealloc];

}

@end

Teacher.h:

#import <Foundation/Foundation.h>

#import "protocolText.h"

@interface Teacher : NSObject<protocolText>

@property(nonatomic,retain) id<protocolText> teaDelegate;

-(void)excise; //叫学生做试卷

-(void)SendPager;

-(void)ReceivePager;//老师收试卷

@end

Teacher.m:

#import "Teacher.h"

@implementation Teacher

-(void)SendPager

{

    NSLog(@"老师发试卷");

}

-(void)excise

{

    if ([_teaDelegate respondsToSelector:@selector(Excise)]) {

        [_teaDelegate Excise];

    }

}

-(void)ReceivePager

{

    NSLog(@"老师收试卷");

}

- (void)dealloc

{

    [_teaDelegate release];

    [super dealloc];

}

@end

main:

#import <Foundation/Foundation.h>

#import "Student.h"

#import "Teacher.h"

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

{

    @autoreleasepool {

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

        Teacher *tea = [[[Teacher alloc] init] autorelease];

        stu.stuDelegate = tea;

        tea.teaDelegate = stu;

        //老师发试卷

        [tea SendPager];

        //老师叫学生做试卷

        [tea excise];

        //学生叫老师收试卷

        [stu receive];

    }

    return 0;

}

结果:

2013-08-05 19:59:47.083 作业3[2817:303] 老师发试卷

2013-08-05 19:59:47.085 作业3[2817:303] 学生做试卷

2013-08-05 19:59:47.086 作业3[2817:303] 老师收试卷

原创粉丝点击