Rによるオブジェクト指向プログラミングの練習

簡単なカウンターの実装。こんな感じかな。

setGeneric("count", function(object, add = 10) standardGeneric("count"))

setClass("Counter1", representation(ct = "numeric", remark = "character"))

setMethod("initialize", "Counter1", function(.Object, ct = 5){
     .Object@ct <- ct
     .Object@remark <- "Hello"
     .Object }
)

setMethod("count", "Counter1", 
   function(object, add){ 
      object@ct <- object@ct + add
      return(object)
   }
)

setMethod("show", "Counter1",
   function(object){ 
      cat(paste("Here, ", object@ct, "\n", sep = ""))
   }
)

counter1 <- new("Counter1")
counter2 <- count(counter1,3)
counter2 <- count(counter2,50)

counter3 <- new("Counter1",ct = 100)
counter3 <- count(counter3)