//
// Phone.h
#import <Foundation/Foundation.h>
typedef enum
{
kFlahlightStatusOpen,
kFlahlightStatusClose
} FlahlightStatus;
// 被继承的这个类我们称之为父类/ 超类
@interface Phone : NSObject
+ (void)carameWithFlahlightStatus:(FlahlightStatus)status;
- (void)carameWithFlahlightStatus:(FlahlightStatus)status;
+ (void)openFlahlight;
+ (void)closeFlahlight;
@end
//
// Phone.m
#import "Phone.h"
@implementation Phone
//可以有同名的类方法和对象方法
- (void)carameWithFlahlightStatus:(FlahlightStatus)status;
{
NSLog(@"- carameWithFlahlightStatus");
}
+ (void)carameWithFlahlightStatus:(FlahlightStatus)status
{
if (status == kFlahlightStatusOpen) {
NSLog(@"%@",self);//Iphone,即使这个方法是子类通过super方法调过来的,self也是子类对象Iphone.
[self openFlahlight];
}else
{
NSLog(@"%@",self);
[self closeFlahlight];
}
NSLog(@"拍照");
}
+ (void)openFlahlight
{
NSLog(@"打开闪光灯");
}
+ (void)closeFlahlight
{
NSLog(@"关闭闪光灯");
}
@end
//
// Iphone.h
#import <Foundation/Foundation.h>
#import "Phone.h"
@interface Iphone : Phone
+ (void)carameWithFlahlightStatus:(FlahlightStatus)status;
- (void)test;
@end
//
// Iphone.m
#import "Iphone.h"
@implementation Iphone
+ (void)carameWithFlahlightStatus:(FlahlightStatus)status
{
// 由于以下代码和父类中的一模一样, 所以只需调用父类写好的代码即可
/*if (status == kFlahlightStatusOpen) {
NSLog(@"%@",self);
[self openFlahlight];
}else
{
NSLog(@"%@",self);
[self closeFlahlight];
}
NSLog(@"拍照");*/
// [self carameWithFlahlightStatus:status];
// 只需要利用super给父类的方法发送一个消息, 那么系统就会自动调用父类的方法
// 如果以后想在子类中调用父类的方法可以使用super
// 如果想在给父类方法进行扩展的同时保留父类的方法, 那么可以使用super调用父类同名的方法
[super carameWithFlahlightStatus:status];
}
- (void)test
{
/*
super跟self一样,super在类方法中就是代表了这个类,会调用父类的类方法。在对象方法中, 代表了这个对象,会调用父类的对象方法,
可以利用super在任意方法中调用父类中的方法,super一般用在重写父类的方法并且想保留父类的功能。
*/
[super carameWithFlahlightStatus:kFlahlightStatusOpen];
}
@end
//
// main.m
#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;
}