当前位置:编程学习 > C/C++ >>

用继承实现XYPoint和Circle两个类

 1 #import <Foundation/Foundation.h>
 2 @protocol show
 3 @required
 4 -(void)printOn;
 5 @end
 6
 7 @inte易做图ce 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 @inte易做图ce 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

补充:软件开发 , C语言 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,