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:
- The R.oo package
- S3 and S4 classes
- Reference classes
- the proto package
我的问题是:
哪些主要差异区分R中的OO编程这些方式?
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.
因此,我要求提供详细信息,以经验为基础,以客观的方式提出,并以事实和参考为后盾.阐明这些方法如何映射到标准OO实践的奖励点.
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
S4类
- 可以分派多个参数
- 实施起来比S3复杂
参考课程
- 避免复制大型对象(通过引用传递)的基本用途
- 使用RefClasses的原因的说明
原始
- ggplot2最初是用原型编写的,但最终将使用S3进行重写.
- 简洁的概念(原型,而不是类),但在实践中似乎很棘手
- 下一版ggplot2似乎正在远离它
- 概念和实现的描述
- ggplot2 was originally written in proto, but will eventually be rewritten using S3.
- Neat concept (prototypes, not classes), but seems tricky in practice
- Next version of ggplot2 seems to be moving away from it
- Description of the concept and implementation
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屋!