对象工厂

R 中的 S3 与 R6 面向对象编程

Richie Cotton

Data Evangelist at DataCamp

 

ch3_1-the-object-factory.003.png

R 中的 S3 与 R6 面向对象编程

 

 

类生成器是对象的模板

R 中的 S3 与 R6 面向对象编程

 

 

类生成器是对象的模板,即工厂

R 中的 S3 与 R6 面向对象编程

ch3_1-the-object-factory.007.png

R 中的 S3 与 R6 面向对象编程

 

ch3_1-the-object-factory.009.png

R 中的 S3 与 R6 面向对象编程

 

ch3_1-the-object-factory.010.png

R 中的 S3 与 R6 面向对象编程

 

library(R6)
thing_factory <- R6Class(
  "Thing",

private = list( a_field = "a value", another_field = 123 )
)
R 中的 S3 与 R6 面向对象编程

敬请期待…

 

 

     public

     active

R 中的 S3 与 R6 面向对象编程

 

a_thing <- thing_factory$new()
another_thing <- thing_factory$new() 
yet_another_thing <- thing_factory$new()
R 中的 S3 与 R6 面向对象编程

小结

  • 加载 R6 包以使用 R6!
  • R6Class() 定义类生成器
  • 类名应为 UpperCamelCase
  • 数据字段存于 private 列表
  • 用工厂的 new() 方法创建对象
R 中的 S3 与 R6 面向对象编程

Passons à la pratique !

R 中的 S3 与 R6 面向对象编程

Preparing Video For Download...