4

有没有办法只列出在类定义中明确定义的引用类的那些方法(与“系统类”继承的那些方法相反,例如refObjectGeneratoror envRefClass)?

Example <- setRefClass(
    Class="Example",
    fields=list(
    ),
    methods=list(
        testMethodA=function() {
            "Test method A"
        },
        testMethodB=function() {
            "Test method B"
        }
    )
)

您当前通过调用该$methods()方法获得的结果(请参阅 参考资料?setRefClass):

> Example$methods()
 [1] "callSuper"    "copy"         "export"       "field"        "getClass"    
 [6] "getRefClass"  "import"       "initFields"   "show"         "testMethodA" 
[11] "testMethodB"  "trace"        "untrace"      "usingMethods"

我在找什么:

> Example$methods()
 [1] "testMethodA" "testMethodB"
4

2 回答 2

3

1)试试这个:

> Dummy <- setRefClass(Class = "dummy")
> setdiff(Example$methods(), Dummy$methods())
[1] "testMethodA" "testMethodB"

2)这是第二种方法,它似乎在这里工作,但你可能想要更多地测试它:

names(Filter(function(x) attr(x, "refClassName") == Example$className, 
    as.list(Example$def@refMethods)))
于 2014-02-18T00:34:48.883 回答
2

不,因为从父类“继承”的引用类中的方法实际上是在生成类时复制到类中的。

setRefClass("Example", methods = list(
  a = function() {}, 
  b = function() {}
))

class <- getClass("Example")
ls(class@refMethods)
#> [1]  "a"            "b"            "callSuper"    "copy"         "export"      
#> [6]  "field"        "getClass"     "getRefClass"  "import"       "initFields"  
#> [11] "show"         "trace"        "untrace"      "usingMethods"

但是您可以找出在父级中也定义的方法并返回:

parent <- getClass(class@refSuperClasses)
ls(parent@refMethods)
#> [1]  "callSuper"    "copy"         "export"       "field"        "getClass"    
#> [6]  "getRefClass"  "import"       "initFields"   "show"         "trace"       
#> [11] "untrace"      "usingMethods"

(请注意,我忽略了您的班级有多个父母的可能性,但这很容易概括)

然后用setdiff()找不同

setdiff(ls(class@refMethods), ls(parent@refMethods))
#> [1] "a" "b"
于 2014-02-18T01:20:22.083 回答