类的使用与继承

//长方形的类的声明与实现
#import
<Foundation/Foundation.h> @interface Rect2 : NSObject { // float chang; // float w; } @property float chang; @property float w; - (float)sizeOfRect; @end #import "Rect2.h" @implementation Rect2 //@synthesize chang; //@synthesize w; - (float)sizeOfRect { NSLog(@"%@", self); return (self.chang)*(self.w); } @end //长方体的类的声明与定义 #import <Foundation/Foundation.h> #import "Rect2.h" @interface LiFangTi : Rect2 { // float height; } @property float height; - (float)tiJiFor; @end #import "LiFangTi.h" @implementation LiFangTi -(float)tiJiFor { return self.w*self.height*self.chang; } @end #import "ViewController.h" #import "LiFangTi.h" #import "Rect2.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; Rect2 *rect=[[Rect2 alloc]init]; rect.chang=1; rect.w =2; NSLog(@"%@", rect); NSLog(@"%f",[rect sizeOfRect]); LiFangTi *liFangTi=[[LiFangTi alloc]init]; liFangTi.chang=1.0; liFangTi.w =3.0; liFangTi.height=3.0; NSLog(@"%f",[liFangTi tiJiFor]); }
posted @ 2014-07-24 08:13  曦阳  阅读(230)  评论(0编辑  收藏  举报