[英]Can I invoke the composite's super's method in Objective-C
Say inherited from a class, I can invoke [self someMethod]
if someMethod is rewritten; 说从类继承,如果someMethod被重写,我可以调用[self someMethod]
; I can also invoke [super someMethod]
. 我也可以调用[super someMethod]
。
What if I use composition instead. 如果我改用合成该怎么办。 Say I have porperty 说我有钱
@propery (nonatomic, strong) XXXController xxController;
Use [self.xxController someMethod]
to invoke someMethod, but how to invoke xxController
's super version someMethod? 使用[self.xxController someMethod]
调用someMethod,但是如何调用xxController
的超级版本someMethod?
No, you can't do this*. 不,您不能这样做*。 Using composition does not allow you to break encapsulation. 使用组合不允许您破坏封装。
*(It is actually possible to do this using runtime functions but you really, really, really shouldn't do that!) *(实际上可以使用运行时函数来执行此操作,但是您确实非常不应该这样做!)
You can't do that. 你不能那样做。 And you don't want to. 而你不想。 Composition styles or not, an object should implement it's own methods, and call it's own super
if necessary. 无论是否具有合成样式,对象都应实现其自己的方法,并在必要时调用其自身的super
。
Instead, you should subclass XXXController
to work how you want, calling super
as necessary in that subclass. 相反,您应该子类XXXController
来按需要工作,并在该子类中根据需要调用super
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.