On 3 January 2018 at 00:52, Duncan Murdoch <murdoch.dun...@gmail.com> wrote: > On 02/01/2018 6:38 PM, Martin Møller Skarbiniks Pedersen wrote: >> >> Hi, >> >> I am trying to understand S3 classes. I have read several tutorials >> about >> the topics but I am still a bit confused. I guess it is because it is >> so different from >> Java OOP. > > > What you do below isn't S3. S3 is a system where the classes are secondary > to the generic functions. Methods "belong" to generics, they don't belong > to classes. As far as I can see, you hardly make use of S3 methods and > generics at all.
Here is my second attempt making a S3-class. But something is wrong with the deposit.account() ? Thanks for any help. I expect this output: [1] 0 Martin Saldo is: 100 but I get this: [1] 100 Martin Saldo is: 0 Regards Martin account <- function(owner = NULL) { if (is.null(owner)) stop("Owner can't be NULL") value <- list(owner = owner, saldo = 0) attr(value, "class") <- "account" value } balance <- function(obj) { UseMethod("balance") } balance.account <- function(obj) { obj$saldo } deposit <- function(obj, amount) { UseMethod("deposit") } deposit.account <- function(obj, amount) { obj$saldo <- obj$saldo + amount } print.account <- function(obj) { cat(obj$owner, "\n") cat("Saldo is: ", obj$saldo, "\n") } A1 <- account("Martin") deposit(A1, 100) balance(A1) print(A1) ______________________________________________ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.