iPhone Objective-C 函数与实例方法相比有何优势?

发布于 2024-12-10 11:02:28 字数 264 浏览 1 评论 0原文

我找到了这篇关于如何逐行读取文件的文章,它使用一个函数来读取每一行:Objective-C:逐行读取文件

我想增加代码重用并将更多代码放入更小的可重用片段中。我应该使用 Objective-C 方法还是函数?我说的是每分钟执行数百次的代码。

谢谢你!

I found this article on how to read a file line by line, and it uses a function to read each line: Objective-C: Reading a file line by line

I'd like to increase my code reuse and put more code into smaller reusable pieces. Should I use Objective-C methods or functions? I'm talking about code that would be executed hundreds of times a minute.

Thank you!

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

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

发布评论

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

评论(2

落叶缤纷 2024-12-17 11:02:28

在方法和函数之间进行选择从来不是可重用性的问题,也很少是速度的问题(无论如何,每分钟数百次意味着每秒几次,这几乎不算什么)。

需要考虑的要点是,

  1. 您是否更愿意思考函数中对对象做某事,还是在对象中做某事听你的指挥?
  2. 您要继承对象并覆盖其行为吗?

如果逐行读取文件,我建议采用一种更简单的方法,当然,除非您正在进行 OOP 作业或只是为了练习而进行 OOP。这并不是说 OOP 有什么问题——它是一项很棒的技术,当您需要对复杂行为进行建模时,请务必使用它。只是对于一个简单的脚本来说,这几乎总是大材小用。

Choosing between methods and functions is never a matter of reusability, and quite rarely a matter of speed (anyway, hundreds of times per minute means several times a second, which is almost nothing).

The main points to consider are

  1. Will you prefer to think in functions doing something to objects or in objects doing something at your command?
  2. Are you going to inherit from the object and override its behaviour?

In case of reading file line by line I would suggest a simpler approach—unless of course you're doing an OOP assignment or doing OOP just for practice. It's not that as if there were anything wrong with OOP—it's a great technique, by all means use it when you need to model complex behaviour. It's just that for a simple script it's almost always an overkill.

谁的年少不轻狂 2024-12-17 11:02:28

使用类和方法。代码重用是面向对象编程的主要目标之一。代码重用也是发明 Objective-C 的主要原因之一,添加C 的类和方法,仅支持函数,因此不支持 OOP。

Use classes and methods. Code reuse is one of the main goals of object oriented programming. Code reuse is also one of the main reasons for which Objective-C was invented, to add classes and methods to C, which only supported functions and thus no OOP.

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