相关疑难解决方法(0)

R引用类作为引用类的字段

我想将引用类传递给引用类构造函数,并将传递的引用类指定为字段.但是,当我运行下面的代码时,我不明白为什么会出现错误.我的问题是:

1)请有人可以解释为什么会发生这种错误:

> a <- ClassA&new()
Error in .getClassFromCache(Class, where) : 
  argument "Class" is missing, with no default
> b <- ClassB$new(a)
Error in .Object$initialize(...) : object 'a' not found
Run Code Online (Sandbox Code Playgroud)

2)我已将class.a.container声明为类"list",但我希望这是一个引用类.我需要放在这里而不是"列表"?

ClassA <- setRefClass(
  "ClassA",

  fields = list(myVar = "numeric"),

  methods = list(
    someMethod = function(){
      print("hi")
    }
  )
)

ClassB <- setRefClass(
  "ClassB",

  fields = list(class.a.container = "list"),

  methods = list(
    initialize = function(class.a){
      class.a.container <<- class.a
  })
)

a <- ClassA&new()
b <- ClassB$new(a)
Run Code Online (Sandbox Code Playgroud)

oop r reference-class

1
推荐指数
1
解决办法
872
查看次数

标签 统计

oop ×1

r ×1

reference-class ×1