我想在我的R S4类中使用多个构造函数。
我有一个具有三个插槽的对象。为了创建该对象,有时我只想直接给出三个插槽的值。但是有时候我想提供一个矩阵,并且我有一个函数可以取一个矩阵并返回这三个槽应该是什么。
起初,看来我可以写一个构造函数。所以我可以写objectFromMatrix(matrix) --> object with three slots
。问题是我也有从该主类继承的子类,并且我也希望能够将这些构造函数与它们一起使用。
因此,我可以为每个子类编写函数作为额外的构造函数,但这会有点乏味并且不像超级OO。
为了使我的问题更加明确,我将尝试在下面编写一个最小的示例。我将用Java编写它,但是我有点生锈,所以让我知道它是否没有道理。
Java中所需的结构:
// An abode is a place where you live and it has a size
class Abode {
int size = 1;
// Main constructor that just assigns args to fields
Abode(int size) {
this.size = size;
}
// Alternative constructor that takes in a different datatype
// and computes args to assign to fields
Abode(string description) {
if(description eq "Large") {
this.size = 5;
}
if(description eq "Small") {
this.size = 1;
}
}
// To keep it simple, a house is just an abode with a name
class House extends Abode {
String name;
House(int size, String name) {
super(size);
this.name = name;
}
House(string size, String name) {
super(size);
this.name = name;
}
}
此实现效果很好,因为我可以调用Abode("big")
或House("big", "Casa de me")
,并且这两个都传递给我在Abode
类中构建的额外构造函数。
跟上房子的比喻,这是我在R语言中所做的最好的事情:
# An abode is a place you live and it has a size
setClass("Abode",
slots =
list(size = "numeric")
)
# Alternative constructor that takes in a different datatype
# and computes args to assign to fields
abode_constructor_2 <- function(sizeString) {
if (sizeString == "big") {return new("Abode", size = 5)}
if (sizeString == "small") {return new("Abode", size = 1)}
}
# A house is an abode with a name
setClass("House",
slots =
list(name = "string"),
contains = "Abode"
)
# I already defined this constructor but I have to do it again
house_constructor_2 <- function(sizeString, name) {
if (sizeString == "big") {return new("House", size = 5, name = name)}
if (sizeString == "small") {return new("House", size = 1, name = name)}
}
在有帮助的情况下,这是出现此问题的实际情况的最小示例。我为Sensor
类sensor_constructor_2
定义了一个额外的构造函数。但是,当我有一个继承自Sensor
的类时,我必须再次构造该构造函数。
# A sensor has three parameters
setClass("Sensor",
slots =
list(Rmin = "numeric", Rmax = "numeric", delta = "numeric")
)
# I also like to make sensors from a matrix
sensor_constructor_2 <- function(matrix) {
params <- matrix_to_params(matrix)
return (new("Sensor", Rmin = params[1], Rmax = params[2], delta = params[3]))
}
# A redoxSensor is just a sensor with an extra field
setClass("redoxSensor",
slots =
list(e0 = "numeric"),
contains = "Sensor"
)
# Goal: make this extra constructor unnecessary by making sensor_constructor_2 a property of the sensor class
extraConstructor_redox <- function(matrix, e0) {
params <- matrix_to_params(matrix)
return (new("redoxSensor", Rmin = params[1], Rmax = params[2], delta = params[3]), e0 = e0)
}
答案 0 :(得分:0)
没有理由为什么您不能使用一个S4构造函数通过使用默认参数和一些额外的逻辑来做到这一点,
setClass("Abode",
slots = list(size = "numeric")
) -> Abode
setClass("House",
slots = list(name = "character"),
contains = "Abode"
) -> House
createDwelling <- function(size=0,name,sizeString){
if(!missing(sizeString)){
if(sizeString == "Large") size <- 5
else if(sizeString == "Small") size <- 1
else stop("invalid sizeString")
}
if(missing(name)) return(Abode(size=size))
else return(House(size=size,name=name))
}
用法示例:
> createDwelling(size=3)
An object of class "Abode"
Slot "size":
[1] 3
> createDwelling(sizeString="Small")
An object of class "Abode"
Slot "size":
[1] 1
> createDwelling(sizeString="Small",name="my house")
An object of class "House"
Slot "name":
[1] "my house"
Slot "size":
[1] 1