object-c的继承

作为面向对象的编程语言,object-c当然也支持继承,而且和java一样,支持的是单一继承,即只有一个超类。
当然,也支持覆盖(或重写)父类的同名方法。例子如下:


 1. 代码
 
//1. MyRectangle.h
#import <Foundation/NSObject.h>


@interface MyRectangle: NSObject {
    int width;
    int height;
}


-(MyRectangle*) initWithWidth: (int) w height: (int) h;
-(void) setWidth: (int) w;
-(void) setHeight: (int) h;
-(void) setWidth: (int) w height: (int) h;
-(int) width;
-(int) height;
-(void) print;
@end

//2. MyRectangle.m

#import "MyRectangle.h"
#import <stdio.h>

@implementation MyRectangle
-(MyRectangle*) initWithWidth: (int) w height: (int) h {
    self = [super init];


    if ( self ) {
        [self setWidth: w height: h];
    }


    return self;
}


-(void) setWidth: (int) w {
    width = w;
}


-(void) setHeight: (int) h {
    height = h;
}


-(void) setWidth: (int) w height: (int) h {
    width = w;
    height = h;
}


-(int) width {
    return width;
}


-(int) height {
    return  height;
}


-(void) print {
    printf( "width = %i, height = %i", width, height );
}
@end


//3. Square.h
#import "MyRectangle.h"


@interface Square: MyRectangle
-(Square*) initWithSize: (int) s;
-(void) setSize: (int) s;
-(int) size;
@end




//4. Square.m
#import "Square.h"


@implementation Square
-(Square*) initWithSize: (int) s {
    self = [super init];


    if ( self ) {
        [self setSize: s];
    }


    return self;
}


-(void) setSize: (int) s {
    width = s;
    height = s;
}


-(int) size {
    return width;
}


-(void) setWidth: (int) w {
    [self setSize: w];
}


-(void) setHeight: (int) h {
    [self setSize: h];
}
@end




//5. main.m
#import "Square.h"
#import "MyRectangle.h"
#import <stdio.h>


int main( int argc, const char *argv[] ) {
    MyRectangle *rec = [[MyRectangle alloc] initWithWidth: 10 height: 20];
    Square *sq = [[Square alloc] initWithSize: 15];


    // print em
    printf( "MyRectangle: " );
    [rec print];
    printf( "\n" );


    printf( "Square: " );
    [sq print];
    printf( "\n" );


    // update square
    [sq setWidth: 20];
    printf( "Square after change: " );
    [sq print];
    printf( "\n" );


    //调用继承自父类的width和height
    printf( "Square after width: %i\n", [sq width] );
    printf( "Square after height: %i", [sq height] );
    // free memory
    [rec release];
    [sq release];
    
    return 0;
}


2. 编译运行
gcc -fconstant-string-class=NSConstantString -c main.m -I /GNUstep/System/Library/Headers
gcc -c MyRectangle.m -I /GNUstep/System/Library/Headers
gcc -c Square.m -I /GNUstep/System/Library/Headers
gcc main.o Square.o MyRectangle.o -o main -L /GNUstep/System/Library/Libraries/ -lobjc -lgnustep-base

$ ./main.exe
MyRectangle: width = 10, height = 20
Square: width = 15, height = 15
Square after change: width = 20, height = 20

Square after width: 20Square after height: 20


3. 说明:
   (1) Square继承自MyRectangle,重写了setWidth和setHeight方法;
   (2) Square可以调用继承自父类MyRectangle的width和height方法。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

liranke

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值