# RollToSuccess Example # Illustrate how to use inheritance to when # creating classes. # Constructor for RollToSuccess base class RollToSuccess <- function( ) { this <- list(history=character(0)) class(this) <- append(class(this), "RollToSuccess") return(this) } # Constructor for derived class Die Die <- function( ) { # Define Die object by first calling # base class constructor this <- RollToSuccess( ) # Append derived class name. class(this) <- append(class(this), "Die") return(this) } # Constructor for derived class Coin Coin <- function( ) { # Define Coin class by first calling # base class constructor this <- RollToSuccess( ) # Append derived class name. class(this) <- append(class(this), "Coin") return(this) } # Define generic reset function. reset <- function(theObject) { UseMethod("reset", theObject) } # Define default reset method. reset.default <- function(theObject) { warning("Default reset method called on unrecognized object.\n") return(theObject) } # Define base class reset method. reset.RollToSuccess <- function(theObject) { theObject$history <- character(0) return(theObject) } # Define generic function. simulation <- function(theObject) { UseMethod("simulation", theObject) } # Define default method. simulation.default <- function(theObject) { warning("Default simulation method called on unrecognized object.\n") return(theObject) } # Define method to run simulation in base class. simulation.RollToSuccess <- function(theObject) { # Reset History. theObject <- reset(theObject) # Perform simulation repeat { thisTrial <- singleTrial(theObject) # cat(thisTrial$result, thisTrial$success, "\n") theObject$history <- c(theObject$history, thisTrial$result) # cat(theObject$history, "\n") if(thisTrial$success) { break } } return(theObject) } # Define generic method. singleTrial <- function(theObject) { UseMethod("singleTrial", theObject) } # Define default method. singleTrial.default <- function(theObject) { warning("Default singleTrial method called on unrecognized object.\n") return(theObject) } # Define base class method. singleTrial.RollToSuccess <- function(theObject) { # Search for next name on class list and execute # singleTrial for that class. NextMethod("singleTrial", theObject) } # Define method for derived class Die. singleTrial.Die <- function(theObject) { roll <- as.character(trunc(runif(1, 1, 6.999999999))) return(list(result=roll, success=(roll=="1"))) } # Define method for derived class Coin. singleTrial.Coin <- function(theObject) { flip <- ifelse(rbinom(1, 1, 0.5), "T", "F") return(list(result=flip, success=(flip=="T"))) } # Define generic method. getHistory <- function(theObject) { UseMethod("getHistory", theObject) } # Define default method. getHistory.default <- function(theObject) { warning("Default getHistory method called on unrecognized object.\n") return(theObject) } # Define base class method. getHistory.RollToSuccess <- function(theObject) { return(theObject$history) } # Test Die and Coin objects. d <- Die( ) c <- Coin( ) d <- reset(d) c <- reset(c) d <- simulation(d) c <- simulation(c) print(d) print(c) getHistory(d) getHistory(c)