如何定义多个变体构造函数和函数,如
function Add( x, y : Integer ) : Integer;
begin
Add := x + y
end;
function Add( s, t : String ) : String;
begin
Add := Concat( s, t )
end;
begin
Writeln(Add(1, 2));
Writeln(Add('Hello, ', 'World!'));
end.
我是否可以通过像
这样的案例裁决来做到这一点A<-setRefClass(Class = "A"
,fields = list(var1="character")
,methods = list(setFields=A.setFields
,initialize=function(var1) {
if(isClass(var1,"B"))
.self$var1<-as.character(var1$getFields("var1"))
else{
.self$var1<-as.character(var1)
}
.self
})
)
如何将函数式编程与objectorientated prgramming结合起来。所以如果我调用函数getFields(vecB),其中vecB是一个向量或对象列表B.返回值应该是每个对象的值?
B.getFields<-function(...,values){
vars<-mget(names(.refClassDef@fieldClasses), envir = attr(.self, ".xData"))
if(missing(values))
return(vars)
if(length(vars)==1)
return(vars[[1]])
return(vars[names(vars) %in% values])
}
B<-setRefClass(Class = "B"
,fields = list(var1 = "character")
)
如何调试,例如函数从A类初始化?我试过了
A$trace("initialize")
a<-A$new("ABC")
initial<-a$initialize
trace(initial,browser,where=A)
但它不起作用。
答案 0 :(得分:3)
使用S4泛型和多态方法
setGeneric("Add", function(x, y) standardGeneric("Add"))
setMethod(Add, c("numeric", "numeric"), function(x, y) x + y)
setMethod(Add, c("character", "character"), function(x, y) paste0(x, y))
所以
> Add(1, 2)
[1] 3
> Add("hello ", "world")
[1] "hello world"
> Add("hello ", 2)
Error in (function (classes, fdef, mtable) :
unable to find an inherited method for function 'Add' for signature '"character", "numeric"'
在引用类中使用此泛型的一个想法是
A <- setRefClass("A",
fields=list(x="ANY"),
methods=list(
getX=function() {
.self$x
}, setX=function(x) {
.self$x <- x
.self
}, addX=function(y) {
setX(Add(getX(), y))
}, show=function() {
cat("class:", class(.self), "\nx:", getX(), "\n")
}))
适当调度功能程序样式:
setMethod(Add, c("A", "ANY"), function(x, y) x$addX(y))
setMethod(Add, c("A", "A"), function(x, y) x$addX(y$getX()))
(也许对于函数式编程,将$addX()
实现为A(.self, x=Add(x$getX(), y))
更有意义,即创建x的克隆?)以允许
> Add(A(x=1), A(x=2))
class: A
x: 3
> Add(A(x="hello "), "world")
class: A
x: hello world
> A(x=2)$addX(3)
class: A
x: 5
虽然不是A(x=1)$addX(A(x=2))
有关返回字段值的一种方法,请参阅this answer:
B = setRefClass("B", fields=c(var1="list", var2="character"),
methods=list(getFields=function(values) {
flds = names(getRefClass()$fields())
if (!missing(values))
flds = flds[flds %in% values]
result = setNames(vector("list", length(flds)), flds)
for (fld in flds)
result[[fld]] = .self[[fld]]
result
}))
以功能方式调用的一种方法需要单独的实现,例如,
setGeneric("getFields", function(x, ...) standardGeneric("getFields"))
setMethod(getFields, "B", function(x, ...) x$getFields(...))
setMethod(getFields, "list", function(x, ...) lapply(x, getFields, ...))
您的课程示例不完整。
A <- setRefClass("A", methods=list(initialize=function(...) {
message("hello A")
callSuper(...)
}))
然后
> A$trace("initialize", browser)
Tracing reference method "initialize" for class "A"
[1] "initialize"
> A()
Tracing .Object$initialize(...) on entry
Called from: eval(expr, envir, enclos)
Browse[1]> n
debug: {
message("hello A")
callSuper(...)
}
Browse[2]> n
debug: message("hello A")
Browse[2]>
答案 1 :(得分:0)
以下是关于R:What are the suggested practices for function polymorphism in R?
中多态性的讨论连接字符串的方法是使用粘贴。
paste("Hello, ","world",sep="")