我有:
MyClass < - setRefClass("MyClass",fields = list(data ="numeric"))
让我们初始化一个对象MyClass:
OBJ < - MyClass(数据= 1:4)
...并在屏幕上打印:
OBJ
Reference class object of class "MyClass"
Field "data":
[1] 1 2 3 4
Run Code Online (Sandbox Code Playgroud)
我想改变它的打印方式,所以我写了这个方法:
print.MyClass < - function(x){cat("This is printed representation:")print(x $ data)}
现在这个工作:
打印(OBJ)
This is printed representation: [1] 1 2 3 4
Run Code Online (Sandbox Code Playgroud)
这不是:
OBJ
有没有办法只用键入来实现我的打印方法OBJ?
我也尝试过show,或者(OBJ),但是对我没有爱.
您可以show在参考类中添加方法,如详细信息?setRefClass.举个例子
MyClass <- setRefClass("MyClass" , fields = list(data="numeric"))
MyClass$methods(show = function(){print("This is printed representation: ")
print(data)})
OBJ <- MyClass(data=1:4)
> OBJ
[1] "This is printed representation: "
[1] 1 2 3 4
Run Code Online (Sandbox Code Playgroud)