为什么“[self class] == [super class]”? [英] Why does "[self class] == [super class]"?

查看:182
本文介绍了为什么“[self class] == [super class]”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我期望 [super class] 返回超类的类,但是我发现,使用这个代码,它返回这个类的类。



代码

  NSLogObject([self class]); 
NSLogObject([super class]);
NSLogObject([self superclass]);
NSLogBool([self class] == [super class]);

输出

  [self class]:MainMenuScene 
[super class]:MainMenuScene
[self superclass]:CCScene
[self class] == [super class ]:[YES]

有人可以解释为什么会发生这种情况吗?我期望它返回与 [self superclass] 相同的值。

 宏: 
-------
#define NSLogBool(i)NSLog(@%s:[%@],#i,(i)?@YES:@NO )
#define NSLogObject(o)NSLog(@%s:[%@],#o,o)


c>调用超级方法当前实例(即 self )。如果self有一个覆盖的版本,那么它会被调用,它会看起来不同。因为你不重写它,调用 [self class] 与调用 [super class] / p>

I expected [super class] to return the superclass's class, however I found, using this code that it returns this class's class.

Code

NSLogObject([self class]);
NSLogObject([super class]);
NSLogObject([self superclass]);
NSLogBool([self class] == [super class]);

Output

[self class]: MainMenuScene
[super class]: MainMenuScene
[self superclass]: CCScene
[self class] == [super class]:[YES]

Can somebody explain why this happens please?. I expect it to return the same value as [self superclass].

Macros:
-------
#define NSLogBool(i)   NSLog(@"%s:[%@]", #i, (i) ? @"YES" : @"NO")
#define NSLogObject(o) NSLog(@"%s:[%@]", #o, o)

解决方案

[super class] calls the super method on the current instance (i.e. self). If self had an overridden version, then it would be called and it would look different. Since you don't override it, calling [self class] is the same as calling [super class].

这篇关于为什么“[self class] == [super class]”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆