私は次のクラス/プロトコルを持っています:
@interface Super
@end
@implementation Super
@end
@interface Sub1
@end
@implementation Sub1
@end
@interface Sub2
@end
@implementation Sub2
@end
@protocol FooProtocol
- (void) foo;
@end
@interface Sub1 (FooCategory) <FooProtocol>
@end
@implementation Sub1 (FooCategory)
- (void) foo {}
@end
@interface Sub2 (FooCategory) <FooProtocol>
@end
@implementation Sub2 (FooCategory)
- (void) foo {}
@end
@interface Super (FooCategory) <FooProtocol>
@end
// Notice that there is no @implementation Super (FooCategory)
これにより、次のような関数を作成できます。
void callFoo(NSArray *supers) {
for (Super *theSuper in supers) {
[theSuper foo];
}
}
Sub1
とSub2
はSuper
2つのサブクラスのみです。私は本質的にカテゴリー法でポリモーフィズムを望んでいます。@interface
forを指定したSuper
が、noを指定した場合@implementation
、clangは警告/エラーを表示しません。
これは本当に悪いハックですか?
潜在的な欠点は何ですか?