액티브 바인딩으로 가져오기와 설정하기

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

Richie Cotton

Data Evangelist at DataCamp

ch3_3-getting-and-setting-with-active-bindings.003.png

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

 

 

     가져오기 = 데이터 필드 읽기

     설정하기 = 데이터 필드 쓰기

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

액티브 바인딩

 

 

     함수처럼 정의

     데이터 변수처럼 접근

R에서 S3와 R6로 배우는 Object-Oriented Programming
thing_factory <- R6Class(
  "Thing",
  private = list(
    ..a_field = "a value",
    ..another_field = 123
  ),
  active = list(

a_field = function() {
if(is.na(private$..a_field)) { return("a missing value") }
private$..a_field
},
another_field = function(value) {
if(missing(value)) { private$..another_field } else {
assert_is_a_number(value)
private$..another_field <- value }
}
) )
R에서 S3와 R6로 배우는 Object-Oriented Programming
a_thing <- thing_factory$new()

a_thing$a_field
"a value"
a_thing$a_field <- "a new value"
Error in (function (value)  : a_field is read-only.
R에서 S3와 R6로 배우는 Object-Oriented Programming

 

a_thing$another_field <- 456
a_thing$another_field <- "456"
Error in (function (value) : is_a_number : 
value is not of class 'numeric'; it has class 'character'.
R에서 S3와 R6로 배우는 Object-Oriented Programming

요약

  • 액티브 바인딩으로 비공개 접근 제어
  • 함수처럼 정의
  • 데이터처럼 접근
R에서 S3와 R6로 배우는 Object-Oriented Programming

연습해 봅시다!

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

Preparing Video For Download...