美文网首页
06-07、super关键字

06-07、super关键字

作者: 山中石头 | 来源:发表于2017-09-20 10:04 被阅读0次
Snip20170920_12.png
main
#import <Foundation/Foundation.h>
#import "Iphone.h"


int main(int argc, const char * argv[]) {

Iphone *p = [Iphone new];
[p test];
[Iphone carameWithFlahlightStatus:kFlahlightStatusOpen];

return 0;
}
Phone.m
#import "Phone.h"

@implementation Phone


- (void)carameWithFlahlightStatus:(FlahlightStatus)status;
{
NSLog(@"- carameWithFlahlightStatus");
}
+ (void)carameWithFlahlightStatus:(FlahlightStatus)status
{
if (status == kFlahlightStatusOpen) {
    [self openFlahlight];
}else
{
    [self closeFlahlight];
}
NSLog(@"拍照");
}

+ (void)openFlahlight
{
NSLog(@"打开闪光灯");
}

+ (void)closeFlahlight
{
NSLog(@"关闭闪光灯");
}

@end
Iphone.m
#import "Iphone.h"

@implementation Iphone

+ (void)carameWithFlahlightStatus:(FlahlightStatus)status
{
NSLog(@"聚焦");
NSLog(@"调光");
NSLog(@"人脸识别");

/*
// 由于以下代码和父类中的一模一样, 所以只需调用父类写好的代码即可
if (status == kFlahlightStatusOpen) {
    [self openFlahlight];
}else
{
    [self closeFlahlight];
}
NSLog(@"拍照");
 */
//    [self carameWithFlahlightStatus:status];
// 只需要利用super给父类的方法发送一个消息, 那么系统就会自动调用父类的方法
// 如果以后想在子类中调用父类的方法可以使用super
// 如果想在给父类方法进行扩展的同时保留父类的方法, 那么可以使用super调用父类同名的方法
[super carameWithFlahlightStatus:status];
}

- (void)test
{
/*
 super在类方法中, 一定会调用父类的类方法
 super在对象方法中, 一定会调用父类的对象方法
 可以利用super在任意方法中调用父类中的方法
 */
[super carameWithFlahlightStatus:kFlahlightStatusOpen];
}
@end

相关文章

网友评论

      本文标题:06-07、super关键字

      本文链接:https://www.haomeiwen.com/subject/jzpnsxtx.html