OC中Super

来源:互联网 发布:日式风格照片 知乎 编辑:程序博客网 时间:2024/06/10 23:45


#import <Foundation/Foundation.h>

/*****************zoombie**********/

@interface Zoombie: NSObject

- (void)walk;

+ (void)test;

- (void)test;

;@end

@implementation Zoombie


- (void)walk{

    NSLog(@"走两步");

}

+ (void)test{

    NSLog(@"Zoombie+test");

}

- (void)test{

    NSLog(@"Zoombie-test");

}


@end

/*-------------jump------------*/

@interface JumpZoombie: Zoombie

- (void)walk;

+ (void)haha;

- (void)haha;

@end

@implementation JumpZoombie

+ (void)haha{

    //self相同调用类方法还是对象方法取决于调用super时的环境  这里调用super时时在类方法的环境中所以调用父类中类方法的test

    [supertest];

}

- (void)haha{

    [supertest];

}


- (void)walk{

    NSLog(@"跳两下");

    //使用调用父类中的walk方法

    [superwalk];

}


@end


int main(intargc, constchar *argv[]) {

    JumpZoombie*p1=[JumpZoombienew];

    [p1walk];

    [JumpZoombiehaha]; //输入test+

    [p1test]; //输出test-

    

    return0;

}

Super使用场景:子类需要重写父类方法的想保留父类的一些行为或在父类基础上衍生

0 0
原创粉丝点击