实现Square类,让其继承自Rectangle类,并在Square类增添新属性和方法
2013-08-21 16:16
411 查看
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
相关文章推荐
- 实现Square类,让其继承自Rectangle类,并在Square类增添新属性和方法
- 实现Square类,让其继承自Rectangle类,并在Square类增添新属性和方法,在2的基础上,在Square类中重写Rectangle类中的初始化和打印方法
- 实现Square类,让其继承自Rectangle类,并在Square类增添新属性和方法,在2的基础上,在Square类中重写Rectangle类中的初始化和打印方法
- JS面向对象(3)之Object类,静态属性,闭包,私有属性, call和apply的使用,继承的三种实现方法
- 面向对象---基于组合的继承:属性的继承基于伪装的方式实现,而方法的继承基于原型链的方式继承。
- JS面向对象(3) -- Object类,静态属性,闭包,私有属性, call和apply的使用,继承的三种实现方法
- JS面向对象(3)之Object类,静态属性,闭包,私有属性, call和apply的使用,继承的三种实现方法
- oop实现方法与属性继承
- Ajax - JavaScript之实现私有属性、像C++和Java一样支持基于类的继承方法之例子
- coffeescript 继承、私有方法、私有属性、公共方法、公共属性、静态方法、静态属性实现
- javascript面向对象编程实现[定义(静态)属性方法--继承]
- javascript面向对象编程实现[定义(静态)属性方法--继承]
- JavaScript 中实现继承的方式(列举3种在前一章,我们曾经讲解过创建类的最好方式是用构造函数定义属性,用原型定义方法。)
- VS2005下自定义用户控件 如何实现属性及方法的相关智能提示
- js继承的实现---object.extend(d,s)方法
- js类,静态类,方法属性继承!
- JS 实现继承静态属性
- JS中的call()和apply()方法- 实现继承
- js实现继承的几种方法
- 多线程实现方式1:自定义一个类,继承Thread类,并重写run方法。