R 中的 S3 与 R6 面向对象编程
Richie Cotton
Data Evangelist at DataCamp
thing_factory <- R6Class(
"Thing",
public = list(
do_something = function() {
message("the parent do_something method")
}
)
)
child_thing_factory <- R6Class( "ChildThing", inherit = thing_factory,public = list( do_something = function() { message("the child do_something method") },do_something_else = function() { message("the child do_something_else method") }))
a_child_thing <- child_thing_factory$new()
a_child_thing$do_something()
the child do_something method
private$ 访问 私有 字段
self$ 访问 自身 的 public 方法
super$ 访问 父类 的 public 方法
child_thing_factory <- R6Class( "ChildThing", inherit = thing_factory, public = list( do_something = function() { message("the child do_something method") }, do_something_else = function() { message("the child do_something_else method")self$do_something()super$do_something()} ) )
a_child_thing <- child_thing_factory$new()
a_child_thing$do_something_else()
the child do_something_else method
the child do_something method
the parent do_something method
self$ 访问 自身 的 public 方法super$ 访问 父类 的 public 方法R 中的 S3 与 R6 面向对象编程