用继承实现XYPoint和Circle两个类

 1 #import <Foundation/Foundation.h>
 2 @protocol show
 3 @required
 4 -(void)printOn;
 5 @end
 6 
 7 @interface XYPoint : NSObject<show>{
 8     int x;
 9     int y;
10 }
11 @property (nonatomic,assign)int x,y;
12 -(id)setX:(int)_x andY:(int)_y;
13 
14 @end
15 
16 #import "XYPoint.h"
17 
18 @implementation XYPoint
19 @synthesize x,y;
20 
21 -(id)setX:(int)_x andY:(int)_y{
22     self.x=_x;
23     self.y=_y;
24     return self;
25 }
26 -(void)printOn{
27     NSLog(@"x = %d ,y = %d",x,y);
28 }
29 @end
30 
31 #import <Foundation/Foundation.h>
32 #import "XYPoint.h" 33 @interface Circle : XYPoint<show>{ 34 int radius; 35 } 36 @property (nonatomic,assign)int radius; 37 -(id)initWithX:(int)_x andY:(int)_y andRadius:(int)_r; 38 -(double)area; 39 -(double)perimeter; 40 @end 41 42 #import "Circle.h" 43 44 @implementation Circle 45 @synthesize radius; 46 -(id)initWithX:(int)_x andY:(int)_y andRadius:(int)_r; 47 { 48 self.x=_x; 49 self.y=_y; 50 self.radius=_r; 51 return self; 52 } 53 54 -(double)area{ 55 return 3.14*radius*radius; 56 } 57 58 -(double)perimeter{ 59 return 2*3,14*radius; 60 } 61 62 -(void)printOn{ 63 NSLog(@"x = %d, y = %d ,radius = %d",x,y,radius); 64 65 } 66 @end

 

posted @ 2013-08-21 16:15  shouqiang Wei  阅读(246)  评论(0编辑  收藏  举报