ios - 为什么 "[self class] == [super class]"?

标签 ios objective-c class oop superclass

我希望 [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] 相同的值。

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

最佳答案

[super class] 在当前实例(即 self)上调用 super 方法。如果 self 有一个覆盖版本,那么它会被调用并且看起来会有所不同。由于您没有覆盖它,因此调用 [self class] 与调用 [super class] 相同。

关于ios - 为什么 "[self class] == [super class]"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11827720/

相关文章:

ios - 将 Json 数据数组添加到 Realm

ios - 显示进度 View 代替状态栏

ios - 我如何在 UIimage 中嵌入标题以导出到电子邮件

java - Java 中多个类的 Varargs 问题

c++ - 在模板类中使用 "this"关键字

ios - 在 Storyboard中重用 UIViewController?

iphone - 将 UIImage 与另一个图像重叠

ios - NSURLCache 真的如本文所述需要初始化吗?

c++ - 在 ios Objective-C 项目中导入、使用和编译单个 C++ 头文件

c# - 是否有必要在 C# 中至少有一个类