调用继承的方法会导致警告而不转换为超类

发布于 11-30 14:03 字数 737 浏览 2 评论 0原文

我有以下继承树:

NSObject <- GameObject <- RenderableObject <- SimpleBullet

GameObject 有一个名为 bounds 的方法,它返回对象的边界。在我的代码中,我有一个 SimpleBullet 调用 bounds 来获取其边界,并且我收到一条警告,指出 bounds 已在多个位置定义;奇怪的。如果我将 SimpleBullet 转换为 GameObject 并调用 bounds 方法,一切都会按预期进行。发生什么事了?我无法弄清楚这种行为。示例:

SimpleBullet* bullet = bulletInstance;
[bullet bounds];    // we get the warning.
[(GameObject*)bullet bounds];    // works as expected.

正如我所说,bounds 方法是在 GameObject 中定义的,但为什么 Obj-C 不知道 SimpleBullet 是一个 GameObject 并且不允许我在没有警告的情况下调用它的方法?

I have the following inheritance tree:

NSObject <- GameObject <- RenderableObject <- SimpleBullet

GameObject has a method called bounds that returns the object's bounds. In my code I have a SimpleBullet that calls bounds to get its bounds, and I receive a warning saying bounds is defined in several places; odd. If I cast the SimpleBullet to a GameObject and call the bounds method, everything works as expected. What's happening? I can't figure out this behaviour. Example:

SimpleBullet* bullet = bulletInstance;
[bullet bounds];    // we get the warning.
[(GameObject*)bullet bounds];    // works as expected.

As I said, the bounds method is defined in GameObject, but why is Obj-C not aware that SimpleBullet is a GameObject and not allowing me to call its method without the warning?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北座城市2024-12-07 14:03:02

经过一番调查,我终于知道发生了什么事。编译器在捉弄我,因为没有报告有意义的错误。问题是我没有导入/包括 SimpleBullet.h。声明了前瞻性声明,但没有真正实施。

这让我认为编译器默认将其视为 id 对象,并尝试在所有注册的类中找到适合调用签名的方法。只是猜测,对此不确定:)。

感谢您的所有帮助。

After some investigation I managed to know what was happening. The compiler was playing tricks on me because did not report a meanfull error. The problem was that I wasn't importing/including SimpleBullet.h. A forward declaration was declared but not the real implementation.

This, leads me to think that by default compiler treats it as an id object, and tries to find a method among all registered classes that suits the call signature. Just a guess, not sure about this :).

Thanks for all the help.

甜点2024-12-07 14:03:02

UIView 还有一个 -bounds 方法。确保您的方法具有相同的返回类型。

UIView also has a -bounds method. Make sure your method has the same return type.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文