使用copy再次实现Circle类,保证不能有内存泄漏问题
2013-08-21 16:31
288 查看
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
相关文章推荐
- 使用copy再次实现Circle类,保证不能有内存泄漏问题
- swipe.js工具包实现手机web滑动轮播效果存在jQuery对象不能使用问题并解决(个人项目经验)
- Vue的项目使用Element ui 走马灯 不能实现的问题
- sqlbulkcopy 使用DataTable作为数据源的数据类型问题--来自数据源的String类型的给定值不能转换为指定目标列的类型 uniqueidentifier
- android 使用Dialog实现Toast效果,解决有的机型不显示Toast的问题(保证能用)
- 使用百分比布局后fresco中SimpleDraweeView不能实现圆形的问题
- css使用text-align: justify不能实现两段对其的问题解决方式
- iOS开发使用 runtime 方法中的 class_copyIvarList,class_copyMethodList 方法时导致内存泄漏问题
- qss 使用hover效果不能实现的奇怪问题
- NSMutableArray使用copy声明所产生内存泄漏的问题
- 使用Apache通过JK实现多Tomcat负载均衡集群时,Apache不能将请求分发给Tomcat处理(即Apache反向代理不成功)的问题
- 如何解决使用ViewDragHelper,实现可拖动控件时,控件不能拖动和不能监听问题?
- 解决VS2005 不能使用同步软件连接2440的问题
- 解决XCode安装插件后插件不能使用的问题
- 求1+2+3+...+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。Java实现
- 使用System.arraycopy()实现数组之间的复制
- 合并有序链表,实现1+2+3...+n,要求不能使用乘除法、循环、条件判断、选择相关的关键字。
- 使用Xcode Instruments Leak解决内存泄漏问题