实现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 width: %d",width);
64 }
65 
66 @end

 

转载于:https://www.cnblogs.com/wsq724439564/p/3272916.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值