Objective-C 方法如何访问被调用者的 ivars?
我正在阅读 Apple 的文档,The Objective-C 编程语言(PDF 链接)。上页。 18,在接收者的实例变量下,我看到了这个。
方法可以自动访问接收对象的实例 变量。您不需要将它们作为参数传递给该方法。 例如,上面所示的
primaryColor
方法不需要 参数,但它可以找到otherRect
的原色并返回 它。每个方法都假设接收者及其实例变量, 无需将它们声明为参数。此约定简化了 Objective-C 源代码。它还支持 面向对象程序员思考对象和消息的方式。 消息发送给接收者就像信件发送给您一样 家。消息参数将外部的信息带入内部 接收者;他们不需要将接收器带到自己身边。
我试图更好地理解他们所描述的内容;这像 Python 的 self 参数或样式吗?
I was reading Apple's documentation, The Objective-C Programming Language (PDF link). On pg. 18, under The Receiver’s Instance Variables, I saw this.
A method has automatic access to the receiving object’s instance
variables. You don’t need to pass them to the method as parameters.
For example, theprimaryColor
method illustrated above takes no
parameters, yet it can find the primary color forotherRect
and return
it. Every method assumes the receiver and its instance variables,
without having to declare them as parameters.This convention simplifies Objective-C source code. It also supports
the way object-oriented programmers think about objects and messages.
Messages are sent to receivers much as letters are delivered to your
home. Message parameters bring information from the outside to the
receiver; they don’t need to bring the receiver to itself.
I am trying to better understand what they are describing; is this like Python's self
parameter, or style?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Objective-C 是 C 的严格超集。
因此 Objective-C 方法“只是”函数指针,实例“只是”C 结构体。
一个方法有两个隐藏参数。第一个是 self(当前实例),第二个是 _cmd(方法的选择器)。
但文档第 18 页描述的是从方法访问类实例变量。
它只是说类的方法可以访问该类的实例变量。
从面向对象的角度来看,它非常基础,但从 C 的角度来看却不是。
它还说您不能从另一个类实例访问实例变量,除非它们是公共的。
Objective-C is a strict superset of C.
So Objective-C methods are "just" function pointers, and instances are "just" C structs.
A method has two hidden parameters. The first one is
self
(the current instance), the second_cmd
(the method's selector).But what the documentation is describing in page 18 is the access to the class instance variables from a method.
It just says a method of a class can access the instance variables of that class.
It's pretty basic from an object-oriented perspective, but not from a C perspective.
It also say that you can't access instance variables from another class instance, unless they are public.
虽然我不会说这是对 Python 的“猛烈攻击”,但它肯定是指 Python 风格的面向对象(老实说,它源自 C 中可用的“伪面向对象”(无论是真正的面向对象与否是另一个论坛的争论))。
值得记住的是,Python 的作用域概念与世界其他地方非常不同——每种方法或多或少都存在于自己的小现实中。这与更多“自我意识”的语言形成对比,这些语言要么具有“this”变量,要么具有某种形式的隐式实例构造。
While I would not say that it is a "slam" against Python, it is most certainly referring to the Python style of Object Orientation (which, in honesty, is derived from the "pseudo-object orientation" available in C (whether it is truly OO or not is a debate for another forum)).
It is good to remember that Python has a very different concept of scope from the rest of the world — each method more or less exists in its own little reality. This is contrasted with more "self-aware" languages which either have a "this" variable or an implicit instance construct of some form.