问题描述
这是关于克隆类,而不是实例化的对象(这是其他一些问题的内容)。目的是为了能够在不影响原来的情况下装饰克隆的类。
我尝试使用子类,但它似乎只有在我还在“装饰器”函数中实例化一个新对象时才有效,否则似乎不会发生继承。
x = R6::R6Class("x",public = list(greeting = "hello"))
y = R6::R6Class("y",public = list(greeting = "bye"))
decorate_and_instantiate = function(class,greet) {
classtobedecorated = R6::R6Class("classtobedecorated",inherit = class)
classtobedecorated$set("public","greet",greet)
classtobedecorated$new()
}
greet1 = decorate_and_instantiate(x,function() cat(self$greeting,"there!"))
greet1$greet()
#>hello there!
greet2 = decorate_and_instantiate(y,function() cat("Ok",self$greeting))
greet2$greet()
#>Ok bye
x$greet()
#>Error: attempt to apply non-function
# Because it is not affected by the decorator,which is what I want
总结一下我所追求的:我想要一种机制来“克隆”x
或 y
以创建一个新类,我可以在该类上调用 $set()
而不会影响 {{ 1}} 或 x
,理想情况下没有上面的继承解决方法。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)