R 和面向对象编程 [英] R and object oriented programming
问题描述
在 R 中以一种或另一种方式进行面向对象的编程是非常可能的.但是,与 Python 不同的是,有很多方法可以实现面向对象:
Object oriented programming in one way or another is very much possible in R. However, unlike for example Python, there are many ways to achieve object orientation:
我的问题是:
在 R 中,这些面向对象编程的方式有哪些主要区别?
What major differences distinguish these ways of OO programming in R?
理想情况下,这里的答案将作为 R 程序员的参考,以帮助他们决定哪种 OO 编程方法最适合他们的需求.
Ideally the answers here will serve as a reference for R programmers trying to decide which OO programming methods best suits their needs.
因此,我要求详细,以客观的方式呈现,基于经验,并以事实和参考为后盾.阐明如何将这些方法映射到标准面向对象实践的加分项.
As such, I am asking for detail, presented in an objective manner, based on experience, and backed with facts and reference. Bonus points for clarifying how these methods map to standard OO practices.
推荐答案
S3 课程
- 不是真正的对象,更多的是命名约定
- 基于 .语法:例如对于打印,
print
调用print.lm
print.anova
等.如果没有找到,print.default
- Not really objects, more of a naming convention
- Based around the . syntax: E.g. for print,
print
callsprint.lm
print.anova
, etc. And if not found,print.default
中四班
- 可以分派多个参数
- 实施起来比 S3 复杂
参考类
- 主要用于避免复制大对象(通过引用传递)
- 使用 RefClasses 的原因描述
原型
- ggplot2 最初是用 proto 编写的,但最终将使用 S3 重写.
- 简洁的概念(原型,而不是类),但在实践中似乎很棘手
- 下一版本的 ggplot2 似乎正在远离它
- 概念和实现的描述莉>
R6 类
- By-reference
- Does not depend on S4 classes
- "Creating an R6 class is similar to the reference class, except that there’s no need to separate the fields and methods, and you can’t specify the types of the fields."
这篇关于R 和面向对象编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!