14.12 Inheritance

  • To inherit behaviour from an existing class, provide the class object via the inherit argument.
  • This example also provides a good example on how to debug an R6 class.
BankAccountOverDraft <- R6Class("BankAccountOverDraft",
  inherit = BankAccount,
  public = list(
    withdraw = function(amount) {
      if ((self$balance - amount) < 0) {
        stop("Overdraft")
      }
      # self$balance() <- self$withdraw()
      self$balance <- self$balance - amount
      invisible(self)
    }
  )
)

14.12.1 Future instances debugging

BankAccountOverDraft$debug("withdraw")
x <- BankAccountOverDraft$new("x", type = "Savings")
x$withdraw(20)

# Turn debugging off
BankAccountOverDraft$undebug("withdraw")

14.12.2 Individual object debugging

  • Use the debug() function.
x <- BankAccountOverDraft$new("x", type = "Savings")
# Turn on debugging
debug(x$withdraw)
x$withdraw(10)

# Turn off debugging
undebug(x$withdraw)
x$withdraw(5)

14.12.3 Test out our debugged class

collinsavings <- BankAccountOverDraft$new("Collin", type = "Savings")
collinsavings
collinsavings$withdraw(10)
collinsavings
collinsavings$deposit(5)
collinsavings
collinsavings$withdraw(5)