다중 상속 단계

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

Richie Cotton

Data Evangelist at DataCamp

ch4_3-multiple-levels-of-inheritance.003.png

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

ch4_3-multiple-levels-of-inheritance.005.png

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

ch4_3-multiple-levels-of-inheritance.006.png

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

 

thing_factory <- R6Class(
  "Thing"
)
child_thing_factory <- R6Class(
  "ChildThing",
  inherit = thing_factory
)
grand_child_thing_factory <- R6Class(
  "GrandChildThing",
  inherit = child_thing_factory
)
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") } )
)
R에서 S3와 R6로 배우는 Object-Oriented Programming

 

grand_child_thing_factory <- R6Class(
  "GrandChildThing",
  inherit = child_thing_factory,
  public = list(
    do_something = function() {
      message("the grand-child do_something method")

super$do_something()
super$super$do_something()
} ) )
R에서 S3와 R6로 배우는 Object-Oriented Programming

 

a_grand_child_thing <- grand_child_thing_factory$new()

a_grand_child_thing$do_something()
the grand-child do_something method

the child do_something method
Error in a_grand_child_thing$do_something(): attempt to apply non-function
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")
    }
  ),

active = list( super_ = function() super )
)
R에서 S3와 R6로 배우는 Object-Oriented Programming

 

grand_child_thing_factory <- R6Class(
  "GrandChildThing",
  inherit = child_thing_factory,
  public = list(
    do_something = function() {
      message("the grand-child do_something method")

super$do_something()
super$super_$do_something()
} ) )
R에서 S3와 R6로 배우는 Object-Oriented Programming

 

a_grand_child_thing <- grand_child_thing_factory$new()

a_grand_child_thing$do_something()
the grand-child do_something method
the child do_something method
the parent do_something method
R에서 S3와 R6로 배우는 Object-Oriented Programming

요약

  • R6 객체는 직접 부모만 접근할 수 있습니다
  • 중간 클래스는 자기 부모를 노출할 수 있습니다
  • super_라는 active 바인딩을 사용하십시오
  • super_super를 그대로 반환하면 됩니다
R에서 S3와 R6로 배우는 Object-Oriented Programming

연습해 봅시다!

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

Preparing Video For Download...