Читать книгу The Big R-Book - Philippe J. S. De Brouwer - Страница 210
Warning – Silent setting to default
ОглавлениеDid you notice that R is silent about the missing balance? This is something to be careful with. If you forget that a default value has been assigned then this might lead to confusing mistakes.
An empty value for balance is not very useful and it can even lead to errors. Therefore, it is possible to assign default values with the function prototype
when creating the class definition.
prototype()
setClass(“CurrAcc”, representation(interest_rate = “numeric”, balance = “numeric”), contains = “Acc”, prototype(holder = NA_character_, interst_rate = NA_real_, balance = 0)) x_account <- new(“CurrAcc”, # no holder # no interest rate # no balance branch = “LDN12”, opening_date= as.Date(“2018-12-01”)) x_account # show what R did: ## An object of class “CurrAcc” ## Slot “interest_rate”: ## numeric(0) ## ## Slot “balance”: ## [1] 0 ## ## Slot “holder”: ## [1] NA ## ## Slot “branch”: ## [1] “LDN12” ## ## Slot “opening_date”: ## [1] “2018-12-01”