实现Square类,让其继承自Rectangle类,并在Square类增添新属性和方法,在2的基础上,在Square类中重写Rectangle类中的初始化和打印方法
2013-08-21 16:17
519 查看
实现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
相关文章推荐
- 实现Square类,让其继承自Rectangle类,并在Square类增添新属性和方法,在2的基础上,在Square类中重写Rectangle类中的初始化和打印方法
- 实现Square类,让其继承自Rectangle类,并在Square类增添新属性和方法
- 实现Square类,让其继承自Rectangle类,并在Square类增添新属性和方法
- 2018/01/07JAVA 基础 / 接口与继承:重写【子类继承并覆盖父类的对象方法】、隐藏【子类继承并覆盖父类的类方法】与实现类实现接口方法の区别
- 面向对象,类的组合关系,继承,实现,方法重写,方法重载,this的使用,抽象方法和抽象类的比较,父类构造方法存在的意义,多态的是用和解析,各种访问修饰符
- JAVA面向对象基础:继承、变量隐藏、方法重写、包、修饰符、this、super
- myeclipse实现Servlet实例(2) 继承GenericServet类实现,需要重写service方法
- Java进阶之路--继承中的方法重写与属性覆盖
- 蓝鸥iOS从零基础到精通就业-OC语言入门 2继承和初始化方法4
- 用java中继承和方法重写实现求最大公约数和最小公倍数
- Swift基础语法: 30 - Swift的基类, 子类, 重写, 重写方法, 重写属性, 防止重写
- MVC继承Controller类并重写OnException方法实现全局错误日志
- coffeescript 继承、私有方法、私有属性、公共方法、公共属性、静态方法、静态属性实现
- C# 继承实现父类方法、重写、重载
- OC基础:继承.初始化方法,便利构造器 分类: ios学习 OC 2015-06-16 19:27 84人阅读 评论(0) 收藏
- Android java基础静态(static)方法的继承和重写,面试常问
- Servlet继承HttpServlet重写service方法的实现!
- java基础(八)Math/代码块/继承成员方法指南的关系/继承中成员变量之间的关系/方法的重写/继承中构造方法之间的关系/this和super的区别
- java中的继承,继承中方法的重写,继承的初始化顺序,final关键字,super关键字
- Swift 继承 override重写父类方法以及属性 final用法