수용, 확장, 재정의

R에서 S3와 R6로 배우는 Object-Oriented Programming

Richie Cotton

Data Evangelist at DataCamp

R에서 S3와 R6로 배우는 Object-Oriented Programming
thing_factory <- R6Class(
  "Thing",
  public = list(
    do_something = function() {
      message("the parent do_something method")
    }
  )
)
R에서 S3와 R6로 배우는 Object-Oriented Programming
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") }
)
)
R에서 S3와 R6로 배우는 Object-Oriented Programming
a_child_thing <- child_thing_factory$new()
a_child_thing$do_something()
the child do_something method
R에서 S3와 R6로 배우는 Object-Oriented Programming

 

 

private$는 private 필드 접근

self$는 self의 public 메서드 접근

super$는 parent의 public 메서드 접근

R에서 S3와 R6로 배우는 Object-Oriented Programming
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()
} ) )
R에서 S3와 R6로 배우는 Object-Oriented Programming

 

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
R에서 S3와 R6로 배우는 Object-Oriented Programming

요약

  • 같은 이름을 주면 재정의(Override)
  • 새 이름을 주면 확장(Extend)
  • self$는 self의 public 메서드 접근
  • super$는 parent의 public 메서드 접근
R에서 S3와 R6로 배우는 Object-Oriented Programming

연습해 봅시다!

R에서 S3와 R6로 배우는 Object-Oriented Programming

Preparing Video For Download...