main函数
// // main.m // OC8继承 // // Created by Zoujie on 15/8/23. // Copyright (c) 2015年 Zoujie. All rights reserved. // #import <Foundation/Foundation.h> #import "ClassA.h" #import "ClassB.h" #import "Classc.h" #import "Rectangle.h" #import "Square.h" #import "XYPoint.h" //不能通过继承删除或减少方法,只能添加 int main(int argc, const char * argv[]) { @autoreleasepool { ClassA *a = [ClassA new]; [a initVar]; [a print]; ClassB *b = [ClassB new]; [b initVar];//继承的方法 [b print]; ClassC *c = [ClassC new]; [c initVar]; [c print]; Rectangle *myRectangle = [Rectangle new]; [myRectangle setWidth:5.71 height:8.52]; NSLog(@"Rectangle:w = %f,h = %f",myRectangle.width,myRectangle.height); NSLog(@"Area = %i , Perimiter = %i",[myRectangle area],[myRectangle perimeter]); Square *mySquare = [Square new]; [mySquare setSide:5]; NSLog(@"Square s = %i",[mySquare side]); NSLog(@"Area = %i , Perimiter = %i",[mySquare area],[mySquare perimeter]); Rectangle *myRect = [Rectangle new]; XYPoint *myPoint = [XYPoint new]; [myPoint setX:50 andY:50]; [myRect setWidth:10 height:20]; myRect.origin = myPoint;//XYPoint 对象赋值给了Rectangle的变量 myPoint 改变 myRect的变量也会改变 NSLog(@"Rectangle w = %i,h = %i",myRect.width,myRect.height); NSLog(@"Origin at (%i,%i)",myRect.origin.x,myRect.origin.y); [myPoint setX:100 andY:200]; [myRect setOrigin:myPoint];//修改Set方法 让每个新创建的origin对象都有她自己的实例 NSLog(@"Rectangle w = %i,h = %i",myRect.width,myRect.height); NSLog(@"Origin at (%i,%i)",myRect.origin.x,myRect.origin.y); NSLog(@"Area = %i,Perimeter = %i",myRect.area,myRect.perimeter); } return 0; }
// // Square.h // OC8继承 // // Created by Zoujie on 15/8/23. // Copyright (c) 2015年 Zoujie. All rights reserved. // #import "Rectangle.h" @interface Square : Rectangle -(void) setSide :(int) s; -(int) side; @end
// // Square.m // OC8继承 // // Created by Zoujie on 15/8/23. // Copyright (c) 2015年 Zoujie. All rights reserved. // #import "Square.h" @implementation Square -(void) setSide:(int)s { [self setWidth:s height:s]; } //对父类的变量直接取值 -(int) side { return self.width; } @end
// // Rectangle.h // OC8继承 // // Created by Zoujie on 15/8/23. // Copyright (c) 2015年 Zoujie. All rights reserved. // #import <Foundation/Foundation.h> @class XYPoint;//使用@class指令提高了效率,因为编译器不需要引入和处理整个XYPoint.h文件是一个类名。如果需要引用XYPloint类的方法(在实现部分中),@class指令是不够的,因为编译器需要更多地消息。 @interface Rectangle : NSObject @property float width,height; -(XYPoint *)origin; -(void) setOrigin:(XYPoint *) pt; -(int) area;//面积 -(int) perimeter;//周长 -(void) setWidth:(float)w height:(float) h; @end
// // Rectangle.m // OC8继承 // // Created by Zoujie on 15/8/23. // Copyright (c) 2015年 Zoujie. All rights reserved. // #import "Rectangle.h" #import "XYPoint.h" @implementation Rectangle { XYPoint *origin;// 在实现部分声明和合成(syntherize)的实例变量是私有的,子类中并不能直接访问,需要明确定义或合成取值方法,才能访问实例变量的值。 } @synthesize width,height; -(void) setWidth:(float)w height:(float) h { width = w; height = h; } -(int)area { return width * height; } -(int)perimeter { return (width+height)*2; } // -(void) setOrigin:(XYPoint *)pt { if (!origin) { origin = [XYPoint new]; } origin.x = pt.x; origin.y = pt.y; // origin = pt; } -(XYPoint *)origin { return origin; } @end
#import <Foundation/Foundation.h> @interface XYPoint : NSObject @property int x,y; -(void) setX:(float) xVal andY:(float) yVal; @end
// // XYPoint.m // OC8继承 // // Created by Zoujie on 15/8/27. // Copyright (c) 2015年 Zoujie. All rights reserved. // #import "XYPoint.h" @implementation XYPoint @synthesize x,y; -(void) setX:(float)xVal andY:(float)yVal { x = xVal; y = yVal; } @end