我想知道在 R5 引用类中定义类方法和类变量的正确方法。
这是一个例子:
> # define R5 class XX
> # member variable: ma
> # member method: mfa
> XX <- setRefClass("XX",
+ fields = list(ma = "character"),
+ methods = list(
+ mfa = function() return(paste(ma, "*"))
+ ))
>
> XX
Generator object for class "XX":
Class fields:
Name: ma
Class: character
Class Methods:
"callSuper", "copy", "export", "field", "getClass", "getRefClass", "import", "initFields",
"mfa"
Reference Superclasses:
"envRefClass"
> # create an instance of XX
> x <- XX$new(ma="ma")
>
> # call member method refering to the member variable.
> x$mfa()
[1] "ma *"
>
> # here, I define *class* variable
> XX$cc <- "cc"
> # contents of XX
> ls(XX)
[1] "cc" "className" "def" "methods" "new"
> # here, I define member method referring to the class var.
> XX$methods(mfc = function() {
+ return(XX$cc)
+ })
> # it does work.
> x$mfc()
[1] "cc"
的XX$cc <- "cc"
行为就像cc
是 XX 的类变量,但我不确定这是否是正确的方法。
例如,XX$def <- "hoge" 可以破坏 XX 类生成器。
所以,我想知道是否有定义类变量和方法的标准方法。
提前致谢。