Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R6类和UseMethod/泛型方法_R_Class_Oop_Generics - Fatal编程技术网

R6类和UseMethod/泛型方法

R6类和UseMethod/泛型方法,r,class,oop,generics,R,Class,Oop,Generics,我想使用R6类和泛型方法(UseMethod)将不同类(Small1、MyClassA和Small2、MyClassB)的小对象添加到MyClass大实例中的公共列表(MyListA和MyListB)中 这在创建时起作用(使用Small1和-2创建Big),但之后失败: > Big$AddObj(Small3) #produces: Error in UseMethod("AddObj", x) : no applicable method for 'AddObj' applied

我想使用R6类和泛型方法(
UseMethod
)将不同类(Small1、MyClassA和Small2、MyClassB)的小对象添加到MyClass大实例中的公共列表(MyListA和MyListB)中

这在创建时起作用(使用Small1和-2创建Big),但之后失败:

> Big$AddObj(Small3) #produces:
Error in UseMethod("AddObj", x) :    no applicable method for 'AddObj'
 applied to an object of class "c('MyClassA', 'R6')"
我不太确定我的错误是什么。以后如何为其他对象调用AddObj方法?如有任何建议,将不胜感激

require('R6')

MyClass <- R6Class("MyClass",
   public = list(
     initialize = function(...) {
       for (x in list(...)) {AddObj(x)}
     },
     AddObj = function(x) {UseMethod("AddObj", x)},
     AddObj.MyClassA = function(x) {
       MyListA <<- c(MyListA, list(x))},
     AddObj.MyClassB = function(x) {
       MyListB <<- c(MyListB, list(x))},
     AddObj.default = function(x) {
       otherObjects <<- c(otherObjects, list(x))},
     Show = function() {
       print(methods(AddObj))
       if (length(MyListA)>0) print(MyListA)
       if (length(MyListB)>0) print(MyListB)
     },
     MyListA = list(),
     MyListB = list(),
     otherObjects = list()
   )
)

MyClassA <- R6Class("MyClassA",
  public = list(
    name = NA,
    initialize = function(input) {
      if (!missing(input)) name <<- as.character(input)}
  ))
MyClassB <- R6Class("MyClassB",
  public = list(
    name = NA,
    initialize = function(input) {
      if (!missing(input)) name <<- as.character(input)}
  ))


Small1 <- MyClassA$new("MyName1")
Small2 <- MyClassB$new("MyName2")
Big <- MyClass$new(Small1, Small2)
Big$Show()

Small3 <- MyClassA$new("MyNewName")
Big$AddObj(Small3)
require('R6'))

MyClassR6对象中没有S3调度,但可以使用if-else语句,如下所示:

library('R6')

MyClass <- R6Class("MyClass",
  portable = FALSE,
  public = list(
    initialize = function(...) {
      for (x in list(...)) {AddObj(x)}
    },
    AddObj = function(x) {
      if (inherits(x, "MyClassA"))
        MyListA <<- c(MyListA, list(x))
      else if (inherits(x, "MyClassB"))
        MyListB <<- c(MyListB, list(x))
      else
        otherObjects <<- c(otherObjects, list(x))
    },
    Show = function() {
      if (length(MyListA)>0) print(MyListA)
      if (length(MyListB)>0) print(MyListB)
    },
    MyListA = list(),
    MyListB = list(),
    otherObjects = list()
  )
)

MyClassA <- R6Class("MyClassA",
  portable = FALSE,
  public = list(
    name = NA,
    initialize = function(input) {
      if (!missing(input)) name <<- as.character(input)}
  ))
MyClassB <- R6Class("MyClassB",
  portable = FALSE,
  public = list(
    name = NA,
    initialize = function(input) {
      if (!missing(input)) name <<- as.character(input)}
  ))



Small1 <- MyClassA$new("MyName1")
Small2 <- MyClassB$new("MyName2")
Big <- MyClass$new(Small1, Small2)
Big$Show()

Small3 <- MyClassA$new("MyNewName")
Big$AddObj(Small3)
Big$Show()
library('R6')

MyClass我发现了一个我不太喜欢的解决方法——通过定义另一个函数(
ExternalAddObj
)和一个
AddObj
的闭包:`ExternalAddObj=function(x){return(AddObj(x))},`从类对象之外的某个地方,使用
Big$ExternalAddObj(Small3)
,它就工作了。不过,还有更好的方法吗?谢谢你,温斯顿!是的,我还意识到您必须进入
Big
环境才能使用S3调度。我还没有决定是选择您的替代方案,还是使用一个只调用AddObj例程的
active
对象。我真的希望保持S3调度,以防我想在其他地方使用这些函数//关于更新:默认的
portable=TRUE
行为非常好,使用
self$
实际上相当直观,我想。嗯,看来我还不能投票支持你的答案。。。只要我有足够的分数就可以了。无论如何,谢谢你。