我有两个协议
@protocol P1
-(void) printP1;
-(void) printCommon;
@end
@protocol P2
-(void) printP2;
-(void) printCommon;
@end
现在,我在一个类中实现这两个协议
@interface TestProtocolImplementation : NSObject <P1,P2>
{
}
@end
我如何编写“printCommon”的方法实现。当我尝试实现时,出现编译时错误。
是否有可能为“printCommon”编写方法实现。
常见的解决方案是分离公共协议并让派生协议实现公共协议,如下所示:
@protocol PrintCommon
-(void) printCommon;
@end
@protocol P1 < PrintCommon > // << a protocol which declares adoption to a protocol
-(void) printP1;
// -(void) printCommon; << available via PrintCommon
@end
@protocol P2 < PrintCommon >
-(void) printP2;
@end
现在采用的类型P1
and P2
还必须采用PrintCommon
的方法来实现收养,您可以安全地通过NSObject<P1>*
通过NSObject<PrintCommon>*
参数。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)