Читать книгу The Big R-Book - Philippe J. S. De Brouwer - Страница 228
Note – Addressing attributes and methods
ОглавлениеThe Reference Class OO implementation R5 uses the $
to access attributes and methods.
It is also possible – though not recommendable – to create methods after creation of the class.
alsoCurrAccount <- setRefClass(“currentAccount”, fields = list( interest_rate = “numeric”, balance = “numeric”), contains = c(“account”) ) alsoCurrAccount$methods(list( credit = function(amnt) { balance <<- balance + amnt }, debet = function(amnt) { if (amnt <= balance) { balance <<- balance - amnt } else { stop(“Not rich enough!”) } } ))