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
相關文章
相關標籤/搜索