Читать книгу The Big R-Book - Philippe J. S. De Brouwer - Страница 201

Note – Difference between inheritance and methods

Оглавление

Note the difference in syntax – for the function setClass – between how the argument representation and the argument contains take values. The representation cand we can create a firstode> argument takes a function and hence, more arguments can be passed by adding them comma separated. In order to passmore than one parent class to contains, one needs to provide a character vector (for example c(“InvAcc”,“Acc”)).

Both the arguments slots and contains will readily use S4 classes and the implicit class of a base type. In order to use S3 classes, one needs first to register them with setOldClass(). If we do not want type control when an instance of a class is generated, we can provide to the slots argument a special class “ANY” (this tell R not to restrict the input).

You might not have noticed right away, but we started off with a complex problem where some objects depend on others (in OO we speak about “parents” and “children”) and even where some objects take others as attributes. Those two things are very different and a little tricky to understand.

At this point, the classes Bnk and Acc exist and we can create a first instance for both.

# Create an instance of Bnk: my_cust_bank <- new(“Bnk”, name = “HSBC”, phone = 123456789) # Create an instance of Acc: my_acc <- new(“Acc”, holder = “Philippe”, branch = “BXL12”, opening_date = as.Date(“2018-10-02”))

The Big R-Book

Подняться наверх