Posing,顾名思义,意思是“冒充”,它跟categories
类似,但本质上不一样,Posing存在的目的在于子类可以冒充父类,使得后续的代码无需把父类修改为子类,就可以很方便的让父类表现成子类的行为,从而实现非常方便的冒充,这在一般的语言中是难以想象的。
在开始在Objective-C中进行构建之前,提醒读者注意,在Mac OS X 10.5中声明已经弃用了冒充(Posing),并且之后无法使用它。 因此对于那些不关心这些弃用方法的人可以跳过本章。
Objective-C允许类完全替换程序中的另一个类。替换类被称为“冒充”目标类。 对于支持冒充的版本,发送到目标类的所有消息都由冒充类接收。
NSObject
包含poseAsClass
- 使我们能够替换现有类的方法,如上所述。
它允许扩展一个类,并且全面的冒充这个超类,比如:有一个扩展NSArray
的NSArrayChild
对象,如果让NSArrayChild
冒充NSArray
,则程序代码所在的NSArray
都会自动替换为NSArrayChild
。
注意,这里不是指代码替换,而是NSArray
所在地方的行为都跟NSArrayChild
一样了。
冒充限制
- 一个类只能构成其直接或间接超类之一。
- 冒充类不得定义目标类中不存在的任何新实例变量(尽管它可以定义或覆盖方法)。
- 目标类在冒充之前可能没有收到任何消息。
- 冒充类可以通过
super
调用重写的方法,从而结合目标类的实现。 - 冒充类可以覆盖类别中定义的方法。
示例代码:
1 #import <Foundation/Foundation.h> 2 3 @interface MyString : NSString 4 5 @end 6 7 @implementation MyString 8 9 - (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target 10 withString:(NSString *)replacement { 11 NSLog(@"The Target string is %@",target); 12 NSLog(@"The Replacement string is %@",replacement); 13 } 14 15 @end 16 17 int main() { 18 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; 19 [MyString poseAsClass:[NSString class]]; 20 NSString *string = @"Test"; 21 [string stringByReplacingOccurrencesOfString:@"a" withString:@"c"]; 22 23 [pool drain]; 24 return 0; 25 }
执行上面示例代码,得到以下结果:
1 2018-11-22 21:23:44.729 Posing[372:306] The Target string is a 2 2018-11-22 21:23:44.730 Posing[372:306] The Replacement string is c
在上面的例子中,只是用实现污染了原始方法,并且这将通过上述方法在所有NSString
操作中受到影响。