Learn R Programming

aoos (version 0.5.0)

Private-class: Private class

Description

This is a virtual class to be contained in other class definitions. It overrides the default subset functions $ and [[ such that private member of a class can not be accessed. Private is every object which has a name with a leading "." (grepl("^\\.", name)). After this check the standard method for class 'envRefClass' is called or an error is reported.

Usage

# S4 method for Private
$(x, name)

# S4 method for Private $(x, name) <- value

# S4 method for Private [[(x, i, j, ...)

# S4 method for Private [[(x, i, j, ...) <- value

Arguments

x
the object
name
name of field or method
value
any object
i
like name
j
ignored
...
ignored

See Also

defineRefClass

Examples

Run this code
ClassWithPrivateField <- defineRefClass({
  Class <- "ClassWithPrivateField"
  contains <- "Private"
  
  .p <- "numeric"
  
  getP <- function() .p
  setP <- function(v) .self$.p <- v
})

test <- ClassWithPrivateField()
stopifnot(inherits(try(test$.p), "try-error"))
stopifnot(inherits(try(test$.p <- 2), "try-error"))
stopifnot(inherits(try(test[[".p"]]), "try-error"))
stopifnot(inherits(try(test[[".p"]] <- 2), "try-error"))

Run the code above in your browser using DataLab