gpt4 book ai didi

引用类字段消失

转载 作者:行者123 更新时间:2023-12-01 11:50:19 27 4
gpt4 key购买 nike

我决定再试一次 Reference Classes,但我的第一个 hello world 已经给我带来了问题。这里出了什么问题?

> memory <- setRefClass(
+ Class = "memory",
+ fields = list(state="vector"),
+ methods = list(
+ get = function() { return(state) },
+ set = function(x) { state <<- x }
+ )
+ )$new()

> memory$set(123)

> print(memory)
Reference class object of class "memory"
Field "state":
[1] 123

> memory$get()
[1] 123

> print(memory)
Reference class object of class "memory"
Field "state":
Error in methods::show(field(fi)) :
error in evaluating the argument 'object' in selecting a method for function 'show': Error in get(name, envir = .self) :
unused argument(s) (name, envir = .self)

最佳答案

我对 Reference Classes 不是很有经验但根据帮助页面 (?ReferenceClasses),我认为您必须向您的类添加一个 show 方法才能自动打印您的对象。

memory <- setRefClass(
Class = "memory",
fields = list(state="vector"),
methods = list(
get = function() { return(state) },
set = function(x) { state <<- x },
show = function() {methods::show(state)}
)
)$new()


memory$set(123)
print(memory)
#[1] 123

memory$get()
#[1] 123


print(memory)
#[1] 123

希望对你有帮助

关于引用类字段消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11816015/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com