类方法在 iOS 中如何工作?

发布于 2024-10-12 06:16:29 字数 529 浏览 1 评论 0原文

我使用过类方法,甚至按照书本编写它们。 我听说过它们被描述为工厂方法。 据我所知,它们对类本身进行操作,而不是对类的实例进行操作。 我也读过它们,但我仍然觉得我没有真正理解它们。

您能否提供任何好的隐喻或关键区别来帮助我自己和其他人理解类方法?

我的部分问题似乎是在类上操作与类实例上操作之间的动态。这仍然是一个棘手的问题,在本次讨论中似乎非常相关。

有人可以尝试将类方法与现实世界的隐喻联系起来吗?

我已经向没有科学背景的朋友解释过这种抵制。它并不完美,但有很大帮助。

想象一下水畅通无阻地流经管道。现在想象我们有一个蝶阀(就像附图一样)。让我们将阀门关闭 80%,这样流过的水就会少得多。阀门“就像”电路中的电阻器,水“就像”电,管道就是电线。增加电阻器的电阻“就像”逐渐关闭流水管道中的阀门。

有没有办法在汽车工厂、花生酱三明治或类似有形的东西之间进行比喻?

感谢您的帮助。

-A

替代文字

I've used class methods and even written them following along with books.
I've heard of them described as factory methods.
I understand that they operate on a class itself, rather than an instance of a class.
I've also read up on them but I still don't feel like I really understand them.

Can you offer any good metaphors or key distinctions that would help myself and others understand class methods?

Part of my problem seems to be the dynamic between when you are operating on a class vs an instance of a class. That remains a slippery issue as well which seems very relevant in this discussion.

Could someone try and relate class methods to a real world metaphor?

I've explained resistance to my friends with no science background like this. It's not perfect but it helps a lot.

Imagine water flowing through a pipe unimpeded. Now imagine we have a butterfly valve (just like the image attached). Let's close the valve 80% of the way so much less water can flow through. The valve is "like" a resistor in a electric circuit and the water is "like" the electricity and the pipe is the wire. To increase the resistance of the resistor is "like" progressively closing the valve in the pipe of flowing water.

Is there a way to draw a metaphor between a car factory, a peanut butter sandwich or something similarly tangible?

Thanks for the help.

-A

alt text

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

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

发布评论

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

评论(1

墨落画卷 2024-10-19 06:16:29

将类视为一台汽水机。罐子(实例)通常是更有用的物品,但机器本身也是您与之交互的东西。当你按下机器上的某些按钮(类方法)时,它会给你罐头。可能还有一些按钮可以告诉您苏打水的价格,或者归还您的硬币,或者机器的创建者(您)认为给机器按钮的任何其他东西。这台机器绝对与罐装不同——它只是定义了你能得到什么样的苏打水。

(为了清楚起见:这实际上并不是示例中的类实例关系。苏打水不是汽水机的实例,就像水实际上不是电一样。但它很好地反映了 与 Objective-C 等语言中的类和实例相关并交互。)

Think of a class as a soda machine. The cans (instances) are generally the more useful item, but the machine itself is a thing that you interact with as well. When you push certain buttons (class methods) on the machine, it will give you cans. There may also be buttons that tell you the price of soda, or return your coin, or anything else the machine's creator (you) thought to give the machine buttons for. The machine is definitely distinct from the cans — it just defines what kind of soda you can get.

(For clarity: This isn't actually a class-instance relationship in the example. Sodas are not instances of a soda machine, just like water is not actually electricity. But it is a good mirror of how you relate to and interact with classes and instances in a language like Objective-C.)

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