zoukankan      html  css  js  c++  java
  • 使用copy再次实现Circle类,保证不能有内存泄漏问题

      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
  • 相关阅读:
    hdu 1290 献给杭电五十周年校庆的礼物 (DP)
    hdu 3123 GCC (数学)
    hdu 1207 汉诺塔II (DP)
    hdu 1267 下沙的沙子有几粒? (DP)
    hdu 1249 三角形 (DP)
    hdu 2132 An easy problem (递推)
    hdu 2139 Calculate the formula (递推)
    hdu 1284 钱币兑换问题 (DP)
    hdu 4151 The Special Number (DP)
    hdu 1143 Tri Tiling (DP)
  • 原文地址:https://www.cnblogs.com/wsq724439564/p/3272961.html
Copyright © 2011-2022 走看看