#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
[super test];
}
- (void)haha {
[super test];
}
- (void)walk {
NSLog(@"跳两下");
//使用调用父类中的walk方法
[super walk];
}
@end
int main(int argc, const char * argv[]) {
JumpZoombie *p1=[JumpZoombie new];
[p1 walk];
[JumpZoombie haha]; //输入test+
[p1 test]; //输出test-
return 0;
}
Super使用场景:子类需要重写父类方法的想保留父类的一些行为或在父类基础上衍生