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

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

 #import <Foundation/Foundation.h>

 @interface Rectangle : NSObject{
int width;
int height;
}
@property int width,height;
-(int) area;
-(int) perimeter;
-(void)setWidth:(int)w andHeight:(int)h;
-(void)print; @end #import <Foundation/Foundation.h> @interface Rectangle : NSObject{
int width;
int height;
}
@property int width,height;
-(int) area;
-(int) perimeter;
-(void)setWidth:(int)w andHeight:(int)h;
-(void)print; @end #import <Foundation/Foundation.h>
#import "Rectangle.h"
@interface Square : Rectangle -(void) setSide:(int) s;
-(int)side;
-(int)area;
-(int)perimeter;
-(void)print; @en #import "Square.h" @implementation Square -(void) setSide:(int) s{
[self setWidth:s andHeight:s];
} -(int) side{
return width;
} -(int)area{
return width*width;
} -(int)perimeter{
return *(width+height);
} -(void) print{
NSLog(@"side width: %d",width);
} @end
上一篇:mysql 数据库 添加查询 修改 删除


下一篇:类中为什么要定义__init__()方法