R和面向对象的编程 [英] R and object oriented programming

查看:86
本文介绍了R和面向对象的编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在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 calls print.lm print.anova, etc. And if not found,print.default

S4类

参考课程

原始

  • 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类

  • 参考
  • 不依赖于S4类
  • "创建 R6类与参考类相似,不同之处在于,无需将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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆