实现Square类,让其继承自Rectangle类,并在Square类增添新属性和方法,在2的基础上,在Square类中重写Rectangle类中的初始化和打印方法

实现Square类,让其继承自Rectangle类,并在Square类增添新属性和方法,在2的基础上,在Square类中重写Rectangle类中的初始化和打印方法

 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 -(void)print;
12 
13 @end
14 
15 
16 #import <Foundation/Foundation.h>
17 
18 @interface Rectangle : NSObject{
19     int width;
20     int height;
21 }
22 @property int width,height;
23 -(int) area;
24 -(int) perimeter;
25 -(void)setWidth:(int)w andHeight:(int)h;
26 -(void)print;
27 
28 @end
29 
30 #import <Foundation/Foundation.h>
31 #import "Rectangle.h"
32 @interface Square : Rectangle
33 
34 -(void) setSide:(int) s;
35 -(int)side;
36 -(int)area;
37 -(int)perimeter;
38 -(void)print;
39 
40 @en
41 
42 #import "Square.h"
43 
44 @implementation Square
45 
46 -(void) setSide:(int) s{
47     [self setWidth:s andHeight:s];
48 }
49 
50 -(int) side{
51     return width;
52 }
53 
54 -(int)area{
55     return width*width;
56 }
57 
58 -(int)perimeter{
59     return 2*(width+height);
60 }
61 
62 -(void) print{
63     NSLog(@"side  %d",width);
64 }
65 
66 @end
原文地址:https://www.cnblogs.com/wsq724439564/p/3272916.html