OC繼承

main函數ide

//
//  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
相關文章
相關標籤/搜索