來源:
思路:
簡單說就是在init的時候檢查自己是否符合預先設計的子類的protocol,這就要求所有子類必須遵守這個protocol,所有針對父類的重載、覆蓋也都以這個protocol為準,protocol以外的方法不允許重載、覆蓋。而在父類的代碼里,可以不必遵守這個protocol,保持了未來維護的靈活性。
好處:
就是避免了父類寫空方法,同時也給子類帶上了緊箍咒:要想當我的孩子,就要遵守這些規矩,不能亂來。業務方在實現子類的時候,就可以根據protocol中的方法去一一實現,然后約定就比較好做了:不允許重載父類方法,只允許選擇實現或不實現protocol中的方法。
比如父類:BaseApiManeger 子類:ApiManager,ApiTestManager
在父類中的聲明協議 SuperMethodProtocol
,在 SuperMethodProtocol
中聲明需要重寫的方法,指定為 @required
或 @optional
#import <Foundation/Foundation.h>
@protocol SuperMethodProtocol <NSObject>
@required
- (void)apiMethod;
@optional
- (void)chiledMethod;
@end
@interface BaseApiManeger : NSObject
// 聲明 child 的屬性,用于父類中調用子類中重載的方法
@property (nonatomic, weak) id<SuperMethodProtocol> child;
@end
父類 .m 文件實現
#import "BaseApiManeger.h"
@implementation BaseApiManeger
- (instancetype)init{
self = [super init];
if (self) {
if ([self conformsToProtocol:@protocol(SuperMethodProtocol)]) {
self.child = (id<SuperMethodProtocol>)self;
}
else{
NSAssert(NO, @"子類必須實現 SuperMethodProtocol ");
}
}
return self;
}
/** 父類中調用子類中的方法 */
- (void)test{
[self.child apiMethod];
}
@end
子類實現
#import "BaseApiManeger.h"
// 遵守協議
@interface ApiManager : BaseApiManeger<SuperMethodProtocol>
@end
@implementation ApiManager
// 實現協議中的方法 -- 重載
- (void)apiMethod{
...
}
- (void)chiledMethod{
...
}
@end
如果子類沒有遵守協議,則會報錯
*** Assertion failure in -[ApiTestManager init], /Users/yons/Desktop/繼承重載/繼承重載/BaseApiManeger.m:21