Lập trình hướng đối tượng với S3 và R6 trong R
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$ truy cập các trường private
self$ truy cập phương thức public trong chính đối tượng
super$ truy cập phương thức public trong lớp cha
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$ truy cập phương thức public trong chính đối tượngsuper$ truy cập phương thức public trong lớp chaLập trình hướng đối tượng với S3 và R6 trong R