Objective-C 后期静态绑定

发布于 2024-09-24 01:19:10 字数 939 浏览 5 评论 0 原文

如果你愿意的话,我自学 Objective-C 是一种罪恶感的乐趣。我自称对 Java 语言有很强的掌握,所以这并不是一个非常困难的过渡 - 但它确实很有趣。但可惜的是,我的问题!

我正在尝试重现 PHP 中存在的东西:后期静态绑定。在 PHP 中,我可以用“static::”修饰方法调用,这会在运行时将该方法动态绑定到调用者。另一方面,如果使用关键字“self::”,则绑定是静态的,并且与它所在的类相关联,无论哪个子类调用它。

在 Obj-C 中,我很难重现这种范例。我问过我的霸主 Google,如何在 Cocoa 中进行后期静态绑定,但我认为这是不可能的。它可能被称为其他名称,或者可能需要一个非常难以理解的解决方法。这就是我现在正在做的事情:

父类方法:

-(id) whoAmI {
 return ([self class]);
}

子类 ChildClass 扩展 ParentClass 并且不会覆盖实例方法 whoAmI。

NSLog(@"Calling from PARENT: %@", [parent whoAmI]);
NSLog(@"Calling from CHILD: %@", [child whoAmI]);

当我将消息发送到每个类对象时,动态绑定会执行它应该执行的操作,并且我从 NSLog() 中得到以下内容:

2010-09-21 11:39:07.484 WhoAmI[4803:a0f]来自家长的电话: 家长
2010-09-21 11:39:07.486 WhoAmI[4803:a0f] Calling from CHILD: Child

最终,我想学习(如果可能的话)如何让 Cocoa 停止动态绑定,以便 whoAmI 方法始终返回它所在的对象(始终是父对象)。我也希望它是一个实例方法。我该怎么做呢?

-肖恩

I'm teaching myself Objective-C as a guilty pleasure, if you would. I have a self-proclaimed strong grasp of the Java language, so it's not a terribly difficult transition – it sure is fun though. But alas, my question!

I'm attempting to reproduce something that exists in PHP: Late Static Binding. In PHP, I can decorate a method call with "static::", which will dynamically bind that method to the caller at runtime. On the other hand, if the keyword "self::" is used, the binding is static and is associated with the class in which it resides, regardless of which child class calls it.

In Obj-C, I'm having difficulty reproducing this paradigm. I've asked my overlord, Google, how to late statically bind in Cocoa, but I don't think it's possible. It may be called something else, or it may require a very over-my-head workaround. Here's what I'm doing now:

Parent Class Method:

-(id) whoAmI {
 return ([self class]);
}

A child class, ChildClass, extends ParentClass and does not override instance method whoAmI.

NSLog(@"Calling from PARENT: %@", [parent whoAmI]);
NSLog(@"Calling from CHILD: %@", [child whoAmI]);

When I send the message to each of the class objects, dynamic binding does what it's supposed to do, and I get the following from NSLog():

2010-09-21 11:39:07.484 WhoAmI[4803:a0f] Calling from PARENT: Parent
2010-09-21 11:39:07.486 WhoAmI[4803:a0f] Calling from CHILD: Child

Ultimately, I want to learn – if possible – how to get Cocoa to stop dynamically binding so that the whoAmI method always returns the object in which it resides (always Parent). I also want it to be an instance method. How would I go about doing this?

-Sean

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

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

发布评论

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

评论(2

奶茶白久 2024-10-01 01:19:10

实际上,Objective C 具有一组强大的自省功能,并且几乎可以肯定可以通过参考 Apple 的广泛 Objective C 运行时文档。这是一个完整的 C API,用于访问 Objective C 对象和对象的内部工作原理。类层次结构。

如果不出意外,通过尝试这些东西,您将学到很多关于该语言如何工作的知识,并且它应该可以帮助您调试困难的问题。

Actually Objective C has a powerful set of introspection features, and it is almost certainly possible to do what you want by referring to Apple's extensive Objective C Runtime documentation. This is a complete C API for accessing the inner workings of Objective C's object & class hierarchy.

If nothing else, by experimenting with this stuff you'll learn a lot about how the language works and it should help you in debugging difficult problems.

半岛未凉 2024-10-01 01:19:10

更改方法以合并父类(或超类)的名称:

-(id) whoAmI {
 return ([Parent class]);    //In this instance, Parent is the superclass
}

碰巧我希望创建的内容必须通过将我的类消息静态绑定到父类本身来实现:Parent。

Change the method to incorporate the name of the Parent class (or the superclass):

-(id) whoAmI {
 return ([Parent class]);    //In this instance, Parent is the superclass
}

It just so happens that what I wished to create had to be brought about by statically binding my class message to the parent class itself: Parent.

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