inst/doc/retListClasses.R

## ----eval=FALSE----------------------------------------------------------
#  constructor <- function(...) {
#    ...
#    out <- list(...)
#    class(out) <- "constructor"
#    out
#  }

## ----eval=FALSE----------------------------------------------------------
#  constructor <- function(...) {
#    ...
#    retList("constructor")
#  }

## ------------------------------------------------------------------------
library("aoos")

Person <- function(name) {
  
  print <- function(x, ...) {
    cat(paste0("Hello, my name is ", .self$name, ".\n"))
  }
  
  retList(c("Person", "Print"))
}

## ------------------------------------------------------------------------
ann <- Person("Ann")
ann

## ------------------------------------------------------------------------
Person <- function(.name) {
  
  print <- function(x, ...) {
    cat(paste0("Hello, my name is ", .self$.name, ".\n"))
  }
  
  name <- function(x) {
    if (!missing(x)) .name <<- x
    .name
  }
  
  retList(c("Person", "Print"))
}

p <- Person("Ann")
p
p$name()
p$name("Paul")
p

## ------------------------------------------------------------------------
Person <- function(name) {
  
  print <- function(x, ...) {
    cat(paste0("Hello, my name is ", .self$name, ".\n"))
  }
  
  retList(c("Person", "Print"))

}

Employee <- function(id, ...) {
  
  print <- function(x, ...) {
    cat("Name: ", name, "\nID:   ", id)
  }
  
  retList("Employee", super = Person(...))
  
}

kalle <- Employee("1", "Kalle")
str(kalle)
kalle

Try the aoos package in your browser

Any scripts or data that you put into this service are public.

aoos documentation built on May 2, 2019, 3:47 p.m.