ObjC第四节:继承

来源:互联网 发布:对电影台词软件 编辑:程序博客网 时间:2024/05/22 15:52

继      承

1、继承

     1.1 NSObject,根类,OC创建的类都继承自根类,位于类层次结构的顶层,没有父类

     1.2 父类的实例变量和方法都可以被隐式的继承过来成为子类的一部分,子类就可以直接访问这些实例变量和方法

     1.3 alloc和init是继承自NSobject的方法

     1.4 继承的概念呈单链继承即单一继承

     1.5 编译器首先去对象所属类中找寻有没有定义与消息同名的方法,如果有则执行,如果没有则去本类的父类寻找,如果有则执行,如果没有则继续往上寻找,如果一直没有找到,就会发出警告

     1.6 继承中,不能删除或减去继承来的方法,但是可以更改方法的实现,即重写方法,必须与父类的方法原型相同,即有相同的返回值类型和参数,此时,调用正确版本的方法基于消息的接收者来确定

     1.7 父类不可以访问子类扩展的方法

2、id

     2.1 typedef  struct objc_object {

  Class  isa;  //表明对象在哪个类

}* id;

     2.2 id是一个通用的对象类型,可以存储任意类型对象

     2.3 isa用来保存对象所属的类,基于动态绑定实现,方法的选择是在程序运行期间根据isa中保存的receiver所属的类来确定的

     2.4 如果是id类型,编译器不会给出警告,因为运行之前不知道id存储的是什么类型的对象,只有程序运行时才能确定

1、组合、继承

XYPoint.h

#import <Foundation/Foundation.h>@interface XYPoint : NSObject@property int x, y;- (void) print;@end
XYPoint.m

#import "XYPoint.h"@implementation XYPoint@synthesize x, y;- (void) print{    NSLog(@"圆心(%d, %d)", x, y);}@end
Shape.h

#import <Foundation/Foundation.h>#import "XYPoint.h"@interface Shape : NSObject//{//    @private   //property自动生成的实例变量是私有的//    int x;//}@property (nonatomic, retain) NSString * color;//@property (nonatomic) int x, y;@property (nonatomic, retain) XYPoint * point;- (id) initWithColor:(NSString *)_color andPoint:(XYPoint *)_point;- (void) draw;+ (void)num;   @end
Shape.m

#import "Shape.h"@implementation Shape//@synthesize color, x, y;@synthesize color, point;- (id) initWithColor:(NSString *)_color andPoint:(XYPoint *)_point{    if (self = [super init])    {        color = _color;        self.point = _point;    }    return self;}- (void) draw{//    NSLog(@"%d, %d", x, y);}+ (void)num{    NSLog(@"++++");}@end
Circle.h

#import <Foundation/Foundation.h>#import "Shape.h"@interface Circle : Shape@property (nonatomic) int radius;- (id) initWithColor:(NSString *)_color andPoint:(XYPoint *)_point andRadius:(int)_radius;- (float) area;+ (void)num;@end
Circle.m

#import "Circle.h"@implementation Circle@synthesize radius;- (id) initWithColor:(NSString *)_color andPoint:(XYPoint *)_point andRadius:(int)_radius{    if (self = [super initWithColor:(NSString *)_color andPoint:(XYPoint *)_point])    {        radius = _radius;    }    return self;}- (float) area{    return 3.14 * radius * radius;}- (void) draw{    [self.point print];    NSLog (@"drawing a circle with r = %d in %@", radius, self.color);}+ (void)num   //类方法可以继承,可以重写,可以super调用{    [super num];    NSLog(@"------");}@end
main.m

#import <Foundation/Foundation.h>#import "Circle.h"int main(){    XYPoint * p = [[XYPoint alloc] init];    p.x = 1;    p.y = 1;    Circle * c = [[Circle alloc] init];    [c initWithColor:@"red" andPoint:p];    [c draw];    [c initWithColor:@"blue" andPoint:p andRadius:5];    [c draw];    p.x = 3;    p.y = 3;    [c initWithColor:@"green" andPoint:p];    [c draw];    [Shape num];    [Circle num];        [c release];    [p release];    return 0;}
2、接第三节练习,子类中初始化父类

SaloonCar.h

#import <Foundation/Foundation.h>#import "Car.h"@interface SaloonCar : Car@property (nonatomic) int windows;- (id) initWithSeats:(int)_seats andHeight:(double)_height andWidth:(float)_width andTair:(Tair *)_tair andWindows:(int)_windows;- (void) print;@end
SaloonCar.m

#import "SaloonCar.h"@implementation SaloonCar@synthesize windows;- (id) initWithSeats:(int)_seats andHeight:(double)_height andWidth:(float)_width andTair:(Tair *)_tair andWindows:(int)_windows{    if (self = [super initWithSeats:_seats andHeight:_height andWidth:_width andTair:_tair])    {        windows = _windows;    }    return self;}- (void) print{    [super print];    NSLog(@"Windows: %d", windows);}- (void)dealloc{    [super dealloc];}@end
mian.m

#import <Foundation/Foundation.h>#import "SaloonCar.h"int main(int argc, const char * argv[]){    @autoreleasepool    {        Tair * t1 = [[Tair alloc] initWithScrews:1 andRadius:1];        [t1 print];        Tair * t2 = [[Tair alloc] init];        [t2 setScrews:2 andRadius:2];        [t2 print];                Car * c1 = [[Car alloc] initWithSeats:1 andHeight:2 andWidth:3 andTair:t1];        [c1 print];        Car * c2 = [[Car alloc] init];        [c2 setTair:t2];        c2.seats = 4;        c2.height = 5;        c2.width = 6;        [c2 print];                SaloonCar * s1 = [[SaloonCar alloc] initWithSeats:1 andHeight:2 andWidth:3 andTair:t1 andWindows:4];        [s1 print];        SaloonCar * s2 = [[SaloonCar alloc] init];        s2.seats = 5;        s2.height = 6;        s2.width = 7;        s2.windows = 8;        s2.tair = t2;        [s2 print];                [t1 release];        [t2 release];        [c1 release];        [c2 release];        [s1 release];        [s2 release];    }    return 0;}
3、id

#import <Foundation/Foundation.h>@interface ClassA : NSObject{    int x;}- (void) initVar;- (void) printVar;@end@implementation ClassA- (void) initVar{    x = 100;}- (void) printVar{    NSLog(@"x in A is: %d", x);}@end@interface ClassB : NSObject{    int x;}- (void) initVar;- (void) printVar;@end@implementation ClassB- (void) initVar{    x = 200;}- (void) printVar{    NSLog(@"x in B is: %d", x);}@endint main(int argc, const char * argv[]){    @autoreleasepool    {        id dataValue;  //id类型的对象,通用指针,泛型        ClassA * a = [[ClassA alloc] init];        ClassB * b = [[ClassB alloc] init];        dataValue = a;  //指针赋值,指向同一堆空间        [dataValue initVar];  //调用A中的方法        [dataValue printVar];        dataValue = b;        [dataValue initVar];        [dataValue printVar];                [b release];        [a release];    }    return 0;}

0 0