我用 R6 包创建了一个银行账户类。它具有私有的余额(生成后无法访问)和提取和存入一定金额的方法以及打印方法。(哈德利·威克姆 14.3.3)
BankAccount <- R6::R6Class("BankAccount", public = list(
# Functions to withdraw and deposit.
deposit = function(amount){
private$balance <- private$balance + amount
},
withdraw = function(amount){
if (amount < private$balance){
private$balance <- private$balance - amount
}
else {
stop("Amount withdrawn is greater than available balance by", -(private$balance - amount))
}
},
initialize = function(initial_balance){
private$balance <- initial_balance
},
print = function(){
cat("Bank balance is equal to", private$balance)
}
),
private = list(
balance = NULL
)
)
目前,如果我创建一个新的 R6 对象并调用大于其初始余额的金额的提款函数,该函数将停止并打印一条消息。相反,我希望直接删除新创建的对象。我怎样才能做到这一点?