Obj-C中的protocol

@protocol NSCopying  //定义协议
    -(id) copyWithZone:(NSZone *zone);
    @end
    //采用协议意味着要承诺实现该协议的所有方法
    /* 对于Obj-C 2.0 中,加入了@optional 和 @required 两个修饰符 
     @protocol BaseballPlayer
     
     -(void) drawHugeSalary  //should be inplemtented
     
     @optional      //not obligatory
     -(void)slideHome;
     -(void)catchBall;
     -(void)throwBall;
     
     @required
     -(void)swingBat;  //should be inplemtented
     @
     */
    //使用非正式协议可以只实现你期望响应的方法
    //非正式协议即类别
 @interface Car : NSObject<NSCopying>
    {
        //implemention the methods
    }
    @end
    
    @interface Car : NSObject<NSCopying, NSCoding> //实现两个协议
    {
        //implemention all of protocol methods
    }
    @end
    
    
    //method of the copy
    // two copt methods
    //1. shallow copy  //copy the pointer  for reference instance
    // use the shallow copy, two pointer will point same instance
    
    //2. deep copy     //copy the instance for reference instance
    //use the deep copy, two instance should be created in heap



你可能感兴趣的:(Obj-C中的protocol)