在R5参考类中定义类方法和类变量
|
我想知道在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\"
的行为就像the2ѭ是XX的类变量一样,但是我不确定这是否是正确的方法。
例如,XX $ def <-\“ hoge \”可能会破坏XX类生成器。
因此,我想知道是否存在定义类变量和方法的标准方法。
提前致谢。
没有找到相关结果
已邀请:
1 个回复
贡炮逗握惫