使用copy再次实现Circle类,保证不能有内存泄漏问题

来源:互联网 发布:淘宝上的桃胶是真的吗 编辑:程序博客网 时间:2024/05/30 05:41
  1 #import <Foundation/Foundation.h>  2 //xieyi  3 @protocol showOn  4 @required   5 -(void)printOn;  6 @end  7 // lei  8 @interface MyPoint : NSObject<showOn,NSCopying>{  9     int x; 10     int y; 11 } 12 @property (nonatomic,assign)int x,y; 13 -(id)initWithX:(int)_x andY:(int)_y; 14 @end 15 // leibie fenlei 16 @interface MyPoint(MoveSet) 17  18 -(void)moveToX:(int)_x andY:(int)_y; 19  20  21 @end 22  23 #import "MyPoint.h" 24  25 @implementation MyPoint 26 @synthesize x,y; 27 -(id)initWithX:(int)_x andY:(int)_y{ 28     if(_x==0) 29         x=1; 30     if(_y==0) 31         y=1; 32     x=_x; 33     y=_y; 34     return self; 35 } 36 -(id)copyWithZone:(NSZone *)zone{ 37     MyPoint* newPoint=[[MyPoint allocWithZone:zone] initWithX:x andY:y]; 38     return newPoint; 39 } 40 -(void)printOn{ 41     NSLog(@" x = %d , y = %d ",x,y); 42 } 43 @end 44 @implementation MyPoint(MoveSet) 45 -(void)moveToX:(int)_x andY:(int)_y{ 46     x=_x; 47     y=_y; 48 } 49 @end 50  51 #import <Foundation/Foundation.h> 52 #import "MyPoint.h" 53  54 @interface Circle : NSObject<showOn>{ 55     MyPoint* point; 56     int radius; 57 } 58 @property (nonatomic,copy)MyPoint* point; 59 @property (nonatomic,assign)int radius; 60 -(id)initWithPoint:(MyPoint* )_p andRadius:(int)_r; 61 -(id)initWithPoint:(MyPoint *)_p; 62  63 @end 64  65  66 #import "Circle.h" 67  68 @implementation Circle 69 @synthesize point; 70 @synthesize radius; 71  72 -(id)initWithPoint:(MyPoint *)_p andRadius:(int)_r{ 73     if(_r==0) 74     { 75         radius=1; 76     } 77     if(_p==nil) 78         return nil; 79     if(self=[super init]) 80     { 81         if(point!=_p) 82         { 83             if(point) 84                 [point release]; 85             point =[[MyPoint alloc]initWithX:_p.x andY:_p.y]; 86         } 87          88     } 89         radius=_r; 90         return self; 91 } 92  93   94 -(id)initWithPoint:(MyPoint*)_p{ 95     self=[self initWithPoint:_p andRadius:10]; 96     return self; 97 } 98  99 -(void)printOn{100     NSLog(@"point(x,y) = (%d,%d),radius = %d",point.x,point.y,radius);     101 }102 103 -(void) dealloc{104     [point release];105     [super dealloc];106 }107 108 @end

原创粉丝点击