Adotar, Estender, Sobrescrever

Programação Orientada a Objetos com S3 e R6 em R

Richie Cotton

Data Evangelist at DataCamp

Programação Orientada a Objetos com S3 e R6 em R
thing_factory <- R6Class(
  "Thing",
  public = list(
    do_something = function() {
      message("the parent do_something method")
    }
  )
)
Programação Orientada a Objetos com S3 e R6 em R
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") }
)
)
Programação Orientada a Objetos com S3 e R6 em R
a_child_thing <- child_thing_factory$new()
a_child_thing$do_something()
the child do_something method
Programação Orientada a Objetos com S3 e R6 em R

 

 

private$ acessa campos privados

self$ acessa métodos públicos em self

super$ acessa métodos públicos no parent

Programação Orientada a Objetos com S3 e R6 em R
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()
} ) )
Programação Orientada a Objetos com S3 e R6 em R

 

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
Programação Orientada a Objetos com S3 e R6 em R

Resumo

  • Sobrescreva usando o mesmo nome
  • Estenda usando um novo nome
  • self$ acessa métodos públicos em self
  • super$ acessa métodos públicos no parent
Programação Orientada a Objetos com S3 e R6 em R

Vamos praticar!

Programação Orientada a Objetos com S3 e R6 em R

Preparing Video For Download...