Objective-C 學習筆記2 類與繼承


編寫一個形狀的類(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

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM