假设 Bob 是人类男性
所以我继承了人类和男性。
现在说我想实现人类作为继承
@interface BobLike : human
然后我想创建一个男性协议
@protocol male
所以,
我们有
@interface BobLike : human <male>
好的,到目前为止一切顺利。通过将男性接口放入协议中,我可以做出类似类的行为。就像是
@protocol male
-(void) growBeard;
-(void) playVideoGames;
@end
@interface human (male) <male>
@end
BobLike 将在这里享受 growBeard 和 playVideogames 的实现
如果后者我想继承男性呢?假设我想创建另一个名为 normalMale 的“类对象”
说 normalMale 有更长的胡须,因此我将覆盖
@protocol normalMale :male
//-(void) growBeard; //No need to be declared because we already declare that as in male protocol
//-(void) playVideoGames; //No need to be declared because we already declare that as in male protocol
@end
@interface NSObject (singleton) <male>
@end
@end
@interface human (normalMale) <normalMale>
@end
然后我重新实现了 -(void) growBeard; -(无效)玩视频游戏;
照常。但是会调用哪一个呢?长胡子,玩视频游戏等?