程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C >> 關於C >> 用繼承實現XYPoint和Circle兩個類

用繼承實現XYPoint和Circle兩個類

編輯:關於C

 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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved