Base R语言类比较:S3 S4 Reference

本文深入探讨R语言的面向对象编程(OOP),包括S3、S4和Reference Class的不同特性,以及它们在创建对象、访问属性和定义方法上的区别。同时,文章对比了S3与S4的优劣,强调了S4的严谨性和团队协作优势。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ref:https://www.datamentor.io/r-programming/object-class-introduction/ 

S3 ClassS4 ClassReferene Class
Lacks formal definitionClass defined using setClass()Class defined using setRefClass()
Objects are created by setting the class attributeObjects are created using new()Objects are created using generator functions
Attributes are accessed using $Attributes are accessed using @Attributes are accessed using $
Methods belong to generic functionMethods belong to generic functionMethods belong to the class
Follows copy-on-modify semanticsFollows copy-on-modify semanticsDoes not follow copy-on-modify semantics

关于R的OOP,还有几个概念:

  • OOP有两个主要的范式,
    • encapsulated OOP, 封装了methods和fields,函数调用形式是object.method(arg1, arg2)
    • functional OOP, methods是泛函数,调用形式是:generic(object, arg2, arg3)。 R语言才用的正是第二种范式。
  • 为什么从S3  S4开始? S3和S4命名是根据S语言的版本命名的,S1和S2版本没有OOP的框架。

判断一个object是什么类型的class,可以安装使用sloop包,使用sloop::otype()

S3 VS S4

S4与S3一脉相承,但是S4比S3更加严谨正式,相对语法也更加繁琐。S4更加适合一个team协作完成一个package。

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值