Reference Class inheriting methods
我正在重新分解包以使用引用类,并遇到方法继承问题。
我有一个类,
如何定义从另一个类继承方法的引用类?继承的方法是通用的有关系吗?
这是一个独立的例子来演示这个问题:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | A <- setRefClass("A", fields=c("f1","f2")) B <- setRefClass("B", fields=c("f3"), contains="A") setGeneric("f", function(.self) standardGeneric("f")) setMethod(f, signature = c(.self="A"), definition = function(.self) { print("Hello from class A") } ) setMethod(f, signature = c(.self="B"), definition = function(.self) { print("Hello from class B") } ) A$methods(f=f) a <- A$new() b <- B$new() |
调用方法:
1 2 3 4 5 6 7 8 | > a$f() [1]"Hello from class A" > b$f() Error in envRefInferField(x, what, getClass(class(x)), selfEnv) : ‘f’ is not a valid field or method name for reference class"B" # Should print"Hello from class B" |
我认为这只是命令的问题:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | A <- setRefClass("A", fields=c("f1","f2")) setGeneric("f", function(.self) standardGeneric("f")) setMethod(f, signature = c(.self="A"), definition = function(.self) { print("Hello from class A") } ) A$methods(f=f) B <- setRefClass("B", fields=c("f3"), contains="A") setMethod(f, signature = c(.self="B"), definition = function(.self) { print("Hello from class B") } ) a <- A$new() b <- B$new() a$f() #[1]"Hello from class A" b$f() #[1]"Hello from class B" |