物件工廠

在 R 中使用 S3 與 R6 的物件導向程式設計

Richie Cotton

Data Evangelist at DataCamp

 

茶壺

在 R 中使用 S3 與 R6 的物件導向程式設計

 

 

類別產生器是物件樣板

在 R 中使用 S3 與 R6 的物件導向程式設計

 

 

類別產生器是物件樣板,也就是工廠

在 R 中使用 S3 與 R6 的物件導向程式設計

微波爐

在 R 中使用 S3 與 R6 的物件導向程式設計

 

工廠

在 R 中使用 S3 與 R6 的物件導向程式設計

 

工廠

在 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 的物件導向程式設計

一起來練習吧!

在 R 中使用 S3 與 R6 的物件導向程式設計

Preparing Video For Download...