Objective-C 编程:学习 C 和/或 Smalltalk 有帮助吗?

发布于 2024-10-05 04:39:10 字数 1431 浏览 7 评论 0原文

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

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

发布评论

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

评论(7

却一份温柔 2024-10-12 04:39:10

Smalltalk 是一种极其紧凑的语言,并且仍然是最纯粹的面向对象语言之一。 Objective-C 是 Smalltalk 和 C 之间的务实妥协,这导致了一些非常实质性的差异。例如,在 Smalltalk 中,一切都是对象——甚至是简单的数字——并且对象的每次操作都是通过消息发送来实现的。消息将以相同的顺序进行评估,无论其名称如何。因此,例如以下内容:

8 + 9 / 23 + 16 * 8

按严格从左到右的顺序进行计算,因为运算符“+”、“/”和“*”对于语言没有特殊含义,只是传递给数字对象的消息。

Objective-C 在 C 中添加了 Smalltalk 风格的对象,但也是 C 的严格超集,保留了 C 的原始类型和内置运算符。因此,在 Objective-C 中,正常的数学运算顺序将应用于上面的表达式 - 首先进行除法和乘法,然后进行加法。

学习 C 对于彻底理解 Objective-C 是绝对必要的。 Objective-C 是 C 的严格超集,并且明确使用完全相同的语法和语义。它将对象的概念移植到 C 上,因为 C 能够保留指向事物的指针,而无需知道如何对事物应用任何操作。然后,它扩展了 C 语法,提供了一种将消息发布到对象以及声明和实现对象可能接收的消息的方法。

Objective-C 运行时的许多通用设计,尤其是与 Cocoa 结合使用时,都直接来自 Smalltalk,包括选择器的概念、使用元类作为类实例工厂、继承的层次结构和系统、模型-视图-控制器(Smalltalk 的原创,尽管现在几乎无处不在)的划分以及在标准集合和对象上定义的许多消息。

在我的脑海中,Smalltalk 的流量控制系统也有很大不同,并且有一个类似但略有不同的“块”概念(尽管大多数较新的实现已使两者保持一致)。 Apple 实际上已经实现了块作为 C 级别的扩展,它被 Objective-C 对象上的许多新方法所利用。

总而言之,Goldberg Smalltalk-80 这本书写得非常好,易于阅读,并且语言非常简单,您只需两到三章即可学会整个语言。大部分复杂性都被运行时可用的对象所吞噬,显然这些东西不会转移。对您的好处是,有关对象和运行时的意识形态内容最终与印刷中的具体细节非常分离。相反,C 使流程控制和算术之类的东西成为一种语言功能,这意味着在您真正感觉自己知道发生了什么之前需要阅读更多语法和更多内容。

因此,总而言之:Smalltalk-80 书(紫色的书)绝对值得一读,并且非常有帮助,但不一定完全相关。学习C无论如何都是必不可少的;我对 K&R 的引用是为了进行比较。

Smalltalk is an incredibly compact language and remains one of the most pure object oriented languages. Objective-C is a pragmatic compromise between Smalltalk and C, which makes for some very substantial differences. For example, in Smalltalk everything is an object — even simple numbers — and every manipulation of an object is by message sending. Messages are evaluated in the same order irrespective of their name. So e.g. the following:

8 + 9 / 23 + 16 * 8

Is evaluated in strict left-to-right order because the operators '+', '/' and '*' have no special meaning to the language being just messages that are passed on to number objects.

Objective-C adds Smalltalk-style objects to C but is also a strict superset of C that retains C's primitive types and built-in operators. So in Objective-C the normal mathematical order of operations would be applied to the expression above — the division and the multiplication would be done first, the additions afterwards.

Learning C is absolutely essential to a thorough understanding of Objective-C. Objective-C is a strict superset of C and explicitly uses exactly the same syntax and semantics as far as they go. It grafts the concept of objects onto C by virtue of C's ability to retain a pointer to a thing without knowing how to apply any operations to the thing. It then extends the C syntax to provide a means for posting messages to objects and for declaring and implementing the messages an object may receive.

A lot of the general design of the Objective-C runtime, especially when coupled with Cocoa, comes directly from Smalltalk, including the concept of a selector, the use of metaclasses as factories for instances of classes, the hierarchy and system of inheritance, the division of model-view-controller (a Smalltalk original, albeit now almost ubiquitous) and a lot of the messages defined on the standard collections and objects.

Off the top of my head, Smalltalk also differs greatly in its system of flow control and has a similar but subtly different idea of a 'block' (though most newer implementations have brought the two into line). Apple have actually implemented blocks as an extension at the C level which is utilised by a lot of the newer methods on Objective-C objects.

That all being said, the Goldberg Smalltalk-80 book is extremely well written, easy to read and the language is so simple that you can learn the whole language in just two or three chapters. Most of the complexity is swallowed by the objects available in the runtime, and obviously that stuff doesn't transfer. The benefit to you is that the ideological stuff about objects and runtimes ends up very separated from the specifics in print. Conversely, C makes stuff like flow control and arithmetic a language feature, which means more syntax and more to read before you really feel you know what's going on.

So, in conclusion: the Smalltalk-80 book (the purple one) is definitely worth a read and extremely helpful but not necessarily entirely relevant. Learning C is essential in any case; my references to K&R are for comparison.

撩起发的微风 2024-10-12 04:39:10

从 Smalltalk 中,您可以学习真正的面向对象编程。像 java、c# 和 Delphi 这样的混合体似乎做得不太好。经过十年的混合,我的编码风格在使用 Smalltalk 几个月后显着改善。

作为一名 iPhone 开发人员,您可能对库的设计和所使用的概念比语法更感兴趣。 c 在那里不会有任何帮助(尽管您需要了解一些基础知识)。使用 Objective C 编程感觉与使用 Smalltalk 编程更加相似。 Smalltalk IDE 远远优于 Objective C IDE,可以帮助您更好地理解面向对象代码的工作原理以及如何构建它。与任何其他面向对象的语言相比,在 Smalltalk (IDE) 中保持代码整洁和良好重构要容易得多。 cocoa 库设计得非常好,至少与 java 或 .net 库相比是这样。它们的状况似乎比 Squeak Smalltalk 的状况要好一些。

From Smalltalk you can learn real object-oriented programming. Hybrids like java, c# and Delphi don't seem to do so well. After ten years of hybrids, my coding style significantly improved after a few months of Smalltalk.

As an iPhone developer, you're probably more interested in the design of the libraries and the concepts used than the syntax. c is not going to be any help there (though you need to understand some basics). Programming in Objective C feels much more similar to programming in Smalltalk. The Smalltalk IDEs are far superior to the Objective C ones, and help you understand much better how object-oriented code works, and how to build it. It is much easier to keep your code clean and well-refactored in a Smalltalk (IDE) than in any other object-oriented language. The cocoa libraries are very well designed, at least when compared to the java or .net ones. They seem to be in somewhat better shape than e.g. the Squeak Smalltalk ones.

顾忌 2024-10-12 04:39:10

我不同意 Knodel 只是一个例子,请参阅此

[someObject message]
并查看

someObject 消息。

您会看到 Objective-C 使用相同的“定位”,是的,它来自 Smalltalk。
学习 Smalltalk 始终是一项很好的时间投资。这个区域的含义是 100%smalltalk 向对象或本身具有某些元类的类发送一些消息。

是的,学习 C 有助于了解如何使用 Objective-C 的一部分,但习惯 OO 并不是 C 教的。因此,了解 C 和 Smalltalk 可以更轻松地使用 Objective-C。但 Objective-C 不仅仅是“力量”来自类库的语言。因此,花时间在这上面肯定是值得的。

是的,您最好了解 C 和 Smalltalk,才能充分利用 Objective-C。

I have to disagree with Knodel just an example see this

[someObject message]
and see

someObject message.

You see Objective-C uses the same "positioning" and yes it comes from Smalltalk.
Learning Smalltalk is always a good investment of time. And the meaning in this area is 100% smalltalk send some message to either an Object or an Class which itself has some MetaClass.

And yes learning C is good to know how to use part of Objective-C but getting used to OO is not taught by C. So knowing C and Smalltalk makes it easier to use Objective-C. BUT Objective-C is not just the language the "power" comes from the class libraries. So spending time on that is surely good spent time.

And yes you better knew C and Smalltalk to make the best out of Objective-C.

椵侞 2024-10-12 04:39:10

免责声明:我不了解 Smalltalk。

我相信你的 Obj-C 技能会从学习 Smalltalk 中受益,但在我看来,你的时间花在学习 C 上会更好。作为在深入研究 C 之前学习 Obj-C 的人,从 Smalltalk 中获取的概念很容易理解接起来,从 C 语言中获取的概念要困难得多。

Disclaimer: I don't know Smalltalk.

I'm sure your Obj-C skills would benefit from learning Smalltalk, but in my opinion, your time would be much better spent learning C. As someone who learned Obj-C before delving into C, the concepts taken from Smalltalk are easy to pick up, the concepts taken from C are much more difficult.

浅忆 2024-10-12 04:39:10

是的,Objective-C 的 Objective 部分与 Smalltalk 非常相似。如果先学习Smalltalk,Objective-C的一些概念会更容易,发送消息的语法也不会那么震撼。然而,我不认为 Smalltalk 一定比 Objective-C 更容易学习,如果你已经了解 C,当然不会,所以你最好直接学习 Objective-C。

话虽如此,在我看来,Smalltalk 是一门不错的语言,而且它本身就值得学习。

Yes, the Objective parts of Objective-C are very similar to Smalltalk. If you learn Smalltalk first, some of the concepts of Objective-C will be easier and the syntax of sending messages will be less of a shock. However, I don't think Smalltalk is necessarily any easier to learn than Objective-C, certainly not if you know C already, so you might as well learn Objective-C straight off.

Having said that, Smalltalk is a nice language IMO and worth learning for its own sake.

來不及說愛妳 2024-10-12 04:39:10

我认为学习Objective-C最好的背景是C。如果你了解C,你会很容易熟悉面向对象编程并用Objective-C编写。

就我个人而言,我不认为学习Smalltalk是一个好主意。

I think that the best background to learn Objective - C is C. If you know C, you'll easily become familiar with object-oriented programming and write in Objective - C.

Personally, I don't think learning Smalltalk us a good idea.

自控 2024-10-12 04:39:10

如果没有 SmallTalk 背景,习惯将消息传递给对象而不是调用方法是非常容易的。然而,SmallTalk 看起来一点也不像 C(除了 SuperCollider 变体),而且该语言甚至将代码块和其他疯狂的东西视为一流对象:例如在 SuperCollider 中 {i <; 5}.while({ // do stuff }) 这种行为并没有出现在 Objective-C 中,可能会让你和我一样感到困惑。

Getting used to passing messages to objects instead of calling methods is pretty easy without a SmallTalk background. However, SmallTalk doesn't look anything like C (except for the SuperCollider variant) and the language even treats code blocks and other crazy stuff as first-class objects: e.g. in SuperCollider {i < 5}.while({ // do stuff }) This behavior did not come over to Objective-C and will likely just confuse you as it does me.

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