times <- setRefClass ("myS5", fields = list (name = "character", now = function() Sys.time() ), methods = list ( initialize = function (){ "初期化のメッセージ" cat ("初期化します\n") # .self <<- initFields(name = "DEFAULT") initFields (name = "デフォルト値") }, reply = function () { "時刻を返す関数" cat (name, "さん,ただいまの時間は", format(now, "%Y年%b %d %a曜日 %X "), "です\n") }, change = function (newName){ "nameフィールドを書き換えます" name <<- newName } ) ) # フィールド一覧を参照する times$fields () # メソッドの一覧を表示する times$methods () # デフォルトでの初期化 obj1 <- times$new () # 引数指定での初期化 obj1 <- times$new (name = "石田") # メソッドの説明を参照する times$help("reply") obj1$reply () # メソッドの説明を参照する times$help("change") obj1$change("鈴木") obj1$reply () # Getter Setter を定義する times$accessors("now", "name") # メソッドの一覧を表示する times$methods () obj1$getNow () obj1$setName ("佐藤") obj1$reply () # メソッドを追加 times$methods ( message = function (x) { cat (x, "さん初めまして\n") } ) obj1$message ("加藤")