黑马程序员——Object-C基础(六)面向对象的特性:继承

------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------

1.     继承的基本用法

例子:写一个矩形类,在写一个继承它的正方形类:

#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

  • 子类方法和属性的访问过程:如果子类没有,就去访问父类的
  • 父类被继承了还是能照常使用的
  • 父类的静态方法
  • 画继承结构图,从子类抽取到父类
  • NSObject的引出:全部OC类的最终父类,包含了一些常用方法,比如+new

 

2.     继承的专业术语

  • 父类\超类  superclass
  • 子类  subclass\subclasses

 

3.     继承的细节

  • 单继承
  • 子类和父类不能有相同的成员变量
  • 方法的重写

 

4.     super关键字

分别调用父类的对象方法和类方法

 

5.     继承的好处

  • 不改变原来模型的基础上,拓充方法
  • 建立了类与类之间的联系
  • 抽取了公共代码
  • 坏处:耦合性强

 

6.     继承的使用场合

  • 它的所有属性都是你想要的,一般就继承
  • 它的部分属性是你想要的,可以抽取出另一个父类

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值