编写一个形状的类(Shape),编写一个他的继承类 长方形(Rectangle)
1、类(Shape)
首先右键工程 [New File],新建一个[Objective-C Class]点击[next]填写类名
Shape
头文件源码如下:
// // Shape.h // sample002 // // Created by on 13-1-13. // Copyright (c) 2013年 echoliu. All rights reserved. // #import <Foundation/Foundation.h> @interface Shape : NSObject{ int length; int width; int height; } //setter 相当于c# java中的set -(void) setLength:(int) l; -(void) setWidth:(int) w; -(void) setHeight:(int) h; //get 相当于c# java中的get
-(int) length;
-(int) width;
-(int) height;
//print 一个方法 打印
-(void) print;
@end
m文件源码如下:
// // Shape.m // sample002 // // Created by echoliu on 13-1-13. // Copyright (c) 2013年 echoliu. All rights reserved. // #import "Shape.h" @implementation Shape //setter 对头文件的实现 -(void) setLength:(int) l{ length=l; } -(void) setWidth:(int) w{ width=w; } -(void) setHeight:(int) h{ height=h; } //getter -(int) length{ return length; } -(int) width{ return width; } -(int) height{ return height; } //print 对头文件的实现 -(void) print{ NSLog(@"this shape length: %i width: %i height: %i",length,width,height); } @end
在main中的测试代码
Shape *shape=[[Shape alloc] init]; [shape setHeight:100]; [shape setWidth:200]; [shape setLength:300]; [shape print];
代码说明
Shape *shape=[[Shape alloc] init]; 初始化一个Shape类,在java c#中,我们一般是Shape shape=new Shape();的方式,而objectc中,一般采用中括号来执行方法,alloc 表示分配内存,init 表示初始化内存。
[shape setHeight:100]; 调用方法 setHeight
最后[run]按钮实现输出
2013-01-14 00:03:00.920 sample002[742:303] this shape length: 300 width: 200 height: 100
2. 类长方形 Rectangle
他继承与Shape类
方法同上新建一个Rectange的object-c class
头文件
// // Rectangle.h // sample002 // // Created by on 13-1-13. // Copyright (c) 2013年 echoliu. All rights reserved. // #import <Foundation/Foundation.h> #import "Shape.h" @interface Rectangle : Shape{ int size; } //setter -(void) setSizeWidth:(int) w withHeight :(int) h; -(void) printSize; @end
实现文件 继承了Shape类,例如里面的print方法,在h文件中并没有定义,当在m文件中确实现了该方法,这个跟其他语言的接口是类似的功能。
// // Rectangle.m // sample002 // // Created by echoliu on 13-1-13. // Copyright (c) 2013年 echoliu. All rights reserved. // #import "Rectangle.h" @implementation Rectangle -(void) setSizeWidth:(int)w withHeight:(int)h{ size=w*h; } -(void) printSize{ NSLog(@"The Size is %d",size); } -(void) print{ NSLog(@"The Child Function Size is %d",size);} @end
main中的调用
#import <Foundation/Foundation.h> #import "Shape.h" #import "Rectangle.h" int main(int argc, const char * argv[]) { @autoreleasepool { Shape *shape=[[Shape alloc] init];//初始化一个shape类 [shape setHeight:100]; [shape setWidth:200]; [shape setLength:300]; [shape print]; // insert code here... NSLog(@"Hello, World!"); //shape for rectangle Rectangle *rect=[[Rectangle alloc] init];
//使用父类方法
[rect setWidth:88];
[rect setHeight:88];
//打印结果
[rect printSize];
[rect print];
//使用类方法
[rect setSizeWidth:200withHeight:300 ];
[rect printSize];
[rect print];
} return 0; }
输出
2013-01-15 20:51:50.178 sample002[1991:303] this shape length: 300 width: 200 height: 100
2013-01-15 20:51:50.181 sample002[1991:303] Hello, World!
2013-01-15 20:51:50.181 sample002[1991:303] The Size is 0
2013-01-15 20:51:50.182 sample002[1991:303] The height widht is 88 88
2013-01-15 20:51:50.182 sample002[1991:303] The Size is 60000
2013-01-15 20:51:50.182 sample002[1991:303] The height widht is 300 200
2013-01-15 20:51:50.183 sample002[1991:303] the zheng fangxing