iPhone Objective-C 函数与实例方法相比有何优势?
我找到了这篇关于如何逐行读取文件的文章,它使用一个函数来读取每一行: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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(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
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.
使用类和方法。代码重用是面向对象编程的主要目标之一。代码重用也是发明 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.