Objective-C 语法与英语有何相似之处?

发布于 2024-11-28 06:45:58 字数 1459 浏览 2 评论 0原文

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

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

发布评论

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

评论(2

土豪 2024-12-05 06:45:58

教授的意思是常见的类 C 编程代码之间的区别,例如:

foo.drawCircle(p, r, c); //C#, C++, …

与常见的 Objective-C 编程代码之间的区别,例如:

[foo drawCircleAtPoint:p withRadius:r andColor:c]; //Objective-C

哪一个可以立即告诉您它在做什么以及参数代表什么?

通过在 Objective-C 中(某种程度上)标记方法参数,即使是复杂的方法(按参数)也可以像简单的英语一样阅读。它们的大小写和空格有所不同,但措辞几乎反映了英语。

Objective-C 并不是特定于语言的(除了 ifdo…while 等)。您可以轻松地将上面的行写为:

[foo zeichneKreisAnPunkt:p mitRadius:r undFarbe:f]; //Objective-C

现在您的代码读起来有点德语。不过,并不是说你应该这样做(说真的,不要这样做!)。


与英语非常相似的语言是 Applescript。但正是这个特性使得 Applescript 使用起来非常痛苦。英语(一般来说是自然语言)根本不是用来指导计算机的。

What the professor meant was the difference between common C-like programming code, such as:

foo.drawCircle(p, r, c); //C#, C++, …

versus common Objective-C programming code, such as:

[foo drawCircleAtPoint:p withRadius:r andColor:c]; //Objective-C

Which one tells you immediately what it's doing and what the parameters stand for?

With method parameters being (kind of) labeled in Objective-C even complex methods (parameter-wise) can be read kind of like plain english. They differ in capitalization and whitespace, but the wording pretty much reflects english language.

Objective-C is not language specific though (apart from if, do…while, etc). You could easily write the line above as:

[foo zeichneKreisAnPunkt:p mitRadius:r undFarbe:f]; //Objective-C

and now your code reads somewhat German. Not that you should do that (seriously, don't!), though.


A language that very much resembles the english language would be Applescript. But it's this very feature that makes Applescript such a pain to work with. English (natural languages in general) simply isn't/aren't meant to be used for instructing computers.

想你只要分分秒秒 2024-12-05 06:45:58

对于您的失望,我真的很抱歉。 比较英语(口头语言)和 Objective-C(编程语言)是不正确的。但在某种程度上,objective-c 的语法比其他编程语言更容易理解。

对于初学者来说,乍一看,他将能够理解代码如何“逻辑上”工作(与 C 或 C++ 相比,他依赖于一些文档)!他无法理解 Objective-C 代码的编程方面,但更容易理解正在发生的事情。

例如,在 C 中,您将拥有用于初始化字符串的 create 方法,并且您可以将该方法命名为 createString(); 在 Objective-C 中,您可以开始编写您可能感觉的代码。

user.text = [NSString stringWithFormat:@"%@ is a %@", string1, string2];

或者

html = [html stringByReplacingOccurrencesOfString:
       [NSString stringWithFormat:@"%@>", text] withString:@""];

有很多例子,只有当你开始与其他编程语言进行比较时,你才会对这种语言感到高兴。我认为你的教授的意思是积极的,以鼓励学生学习。

I am really sorry for your disappointment. It is not right to compare English (Verbal Language) and Objective-C (Programming Language). But to some extent the syntax of objective-c is more understandable than other programming language for that matter.

On first glimpse for a beginner, he will be able to understand how the code works "Logically" (compared to C or C++, where he is dependent on some documentation)! He would not be able to understand the programming aspects of the code in Objective-C, but its easier to comprehend whats going on.

For example, in C, you will have create methods for initializing a string, and you can name the method like createString(); In objective-C, you can start writing code like how you might feel.

user.text = [NSString stringWithFormat:@"%@ is a %@", string1, string2];

or

html = [html stringByReplacingOccurrencesOfString:
       [NSString stringWithFormat:@"%@>", text] withString:@""];

There are so many examples, and you will be happy for this language only if you start comparing with other programming language. I think your professor meant that in a positive way so as to encourage the students to learn.

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