实现Square类,让其继承自Rectangle类,并在Square类增添新属性和方法

 1 #import <Foundation/Foundation.h>
 2 
 3 @interface Rectangle : NSObject{
 4     int width;
 5     int height;
 6 }
 7 @property int width,height;
 8 -(int) area;
 9 -(int) perimeter;
10 -(void)setWidth:(int)w andHeight:(int)h;
11 
12 @end
13 
14 
15 #import "Rectangle.h"
16 
17 @implementation Rectangle
18 @synthesize width,height;
19 -(void)setWidth:(int)w andHeight:(int) h{
20     width=w;
21     height=h;
22 }
23 -(int) area{
24     return width*height;
25 }
26 -(int) perimeter{
27     return (width+height)*2;
28 }
29 
30 @end
31 
32 
33 
34 #import <Foundation/Foundation.h>
35 #import "Rectangle.h"
36 @interface Square : Rectangle
37 -(void) setSide:(int) s;
38 -(int)side;
39 -(int)area;
40 -(int)perimeter;
41 
42 @end
43 
44 
45 #import "Square.h"
46 
47 @implementation Square
48 
49 -(void) setSide:(int) s{
50     [self setWidth:s andHeight:s];
51 }
52 
53 -(int) side{
54     return width;
55 }
56 
57 -(int)area{
58     return width*width;
59 }
60 
61 -(int)perimeter{
62     return 2*(width+height);
63 }
64 @end

 

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