[英]Difference between self and super
just had a noob question. 刚刚有一个菜鸟问题。 I'm trying to understand the difference between calling self and super.
我试图了解调用self和super之间的区别。 Now I understand inheritance and other fundamental OOP concepts, but the idea of self and super is still not clear to me.
现在,我了解了继承和其他基本的OOP概念,但是对我而言,自我和超级的概念仍然不清楚。 I'll illustrate my question with an example.
我将通过一个例子来说明我的问题。
So the the below code performs a segue when the phone is tilted upside-down. 因此,当手机上下颠倒倾斜时,以下代码将执行锁定。 I understand that "Scene2ViewController" is a subclass of "UIViewController" and so "Scene2ViewController" inherits all of UIViewController's methods.
我知道“ Scene2ViewController”是“ UIViewController”的子类,因此“ Scene2ViewController”继承了UIViewController的所有方法。 And so below I'm calling the method performSegueWithIdentifier with the receiver of the message being self.
因此,下面我以消息的接收者为self的方式调用performPergueWithIdentifier方法。 Now when I change "self" to "super" the code still executes the same way.
现在,当我将“自我”更改为“超级”时,代码仍以相同的方式执行。 Isn't calling super the same as calling self?
叫super和叫self不一样吗? If someone could explain this to me it would be appreciated, thanks.
如果有人可以向我解释这一点,将不胜感激。
//Scene2ViewController.m
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation
{
if (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
[self performSegueWithIdentifier:@"SegueToScene1" sender:self];
}
return (interfaceOrientation ==
UIInterfaceOrientationPortrait);
}
self
and super
actually both point to the same object. self
和super
实际上都指向同一个对象。 super
is a keyword that tells the compiler to generate instructions that start the search for a method definition in the super class rather than in the current class. super
是一个关键字,它告诉编译器生成指令,这些指令开始在super类而不是当前类中搜索方法定义。
@interface A : NSObject {}
- (void)foo;
@end
@implementation A
- (void)foo {
NSLog(@"A's foo!");
}
@end
@interface B : A
@end
@implementation B
- (void)foo {
NSLog(@"B's foo!");
}
@end
//...somewhere in a method of class B...
[self foo]; // prints "B's foo" in the console
[super foo]; // prints "A's foo" in the console
If we assume, per the comment, that the last lines are somewhere in a method of B, then self
points to some instance of B. super
also points to that same instance of B. But when you use self
to call foo
, the search for an implementation of foo
starts with class B. When you use super
, the search for a foo
starts with B's superclass, A. 如果我们根据注释假定最后一行位于B方法的某处,则
self
指向B的某个实例super
也指向B的同一实例。但是当您使用self
调用foo
,搜索foo
的实现始于类B。当您使用super
,对foo
的搜索始于B的超类A。
super
is especially handy when you want to preserve the inherited behavior, but add something on. 当您想保留继承的行为,但要添加一些东西时,
super
特别方便。 So, we could have B's implementation of foo
call A's version using [super foo]
. 因此,我们可以让B的
foo
实现通过[super foo]
调用A的版本。 Without super
there'd be no way to call the inherited method, and calling foo
from the overridden method would result in infinite recursion. 如果没有
super
,就无法调用继承的方法,而从被覆盖的方法中调用foo
将导致无限递归。
When you call a method of self
(or rather send a message to self
in Objective-C terms) the runtime will search for an implementation of that method in the inheritance hierarchy, starting with self
, going up to NSObject
. 当您调用
self
方法(或者用Objective-C术语向self
发送消息)时,运行时将在继承层次结构中搜索该方法的实现,从self
开始,直至NSObject
。 So if self
implemented that method, it will be executed. 因此,如果
self
实现该方法,它将被执行。 If not, the super
class will be checked and so on. 如果没有,将检查
super
类,依此类推。
Sending the message to super
is very similar, with the exception that the runtime will start looking for an implementation in super
and skip self
. 发送消息到
super
非常相似,除了运行时将开始在super
寻找实现并跳过self
。
Well sometimes, in a subclass, you might override a function that was already defined in the parent class. 有时候,在子类中,您可能会覆盖父类中已经定义的函数。 Frequently this happens in the init function.
通常,这在init函数中发生。 So if you need to call the parent class's init function you call super.
因此,如果您需要调用父类的init函数,则可以调用super。 If you need the subclass' function you call self.
如果您需要子类的功能,则可以调用self。 If only the parent has the function declared then Self and super act the same.
如果只有父对象声明了该函数,则Self和super的行为相同。 But if only the subclass has the declaration then you cannot call the function from super.
但是,如果只有子类具有声明,那么您将无法从super调用函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.