Hello,
How do I inherit two classes using the R.oo package. Below is kind of a
silly example, but I am trying to create class PerDog from classes Dog and
Person. Error at bottom. I've tried a few other ways of using extend(), but
nothing seems to get me what I want.
Example:
setConstructorS3("Person", function(age=NA) {
this = extend(Object(), "Person",
.age=age
)
this
})
setMethodS3("getAge", "Person", function(this, ...) {
this$.age;
})
setMethodS3("setAge", "Person", function(this,num, ...) {
this$.age = num;
})
# ..........................................
setConstructorS3("Dog", function(dog_age=NA) {
this = extend(Object(), "Dog",
.dog_age=dog_age
)
this
})
setMethodS3("getDogAge", "Dog", function(this, ...) {
this$.dog_age;
})
setMethodS3("setDogAge", "Dog", function(this,num, ...) {
this$.dog_age = num;
})
#..........................................................
setConstructorS3("PerDog", function(age=NA,wt=NA,dog_age=NULL) {
extend(Person(age=age),Dog(dog_age=dog_age), "PerDog",
.wt=wt
)
})
setMethodS3("getWeight", "PerDog", function(this, ...) {
this$.wt;
})
setMethodS3("setWeight", "PerDog", function(this,w, ...) {
this$.wt = w;
})
> pd = PerDog(67,150,1)
Error in list(`PerDog(67, 150, 1)` = <environment>, `extend(Person(age
age), Dog(dog_age = dog_age), "PerDog", .wt = wt)` =
<environment>, :
[2011-10-27 09:34:06] Exception: Missing name of field #1 in class
definition: Dog: 0x73880408
at throw(Exception(...))
at throw.default("Missing name of field #", k, " in class
definition: ",
...className)
at throw("Missing name of field #", k, " in class definition:
",
...className)
at extend.Object(Person(age = age), Dog(dog_age = dog_age),
"PerDog", .wt
= wt)
at extend(Person(age = age), Dog(dog_age = dog_age), "PerDog", .wt =
wt)
at PerDog(67, 150, 1)
Three (of many) other things I have tried:
1)
setConstructorS3("PerDog", function(age=NA,wt=NA,dog_age=NULL) {
this = extend(extend(Person(age=age),
"PerDog"),Dog(dog_age=dog_age),
"PerDog",
.wt=wt
)
this
})
2)
setConstructorS3("PerDog", function(age=NA,wt=NA,dog_age=NULL) {
this = extend(Dog(dog_age=dog_age), "PerDog",
.wt=wt
)
this
})
setConstructorS3("PerDog", function(age=NA,wt=NA,dog_age=NULL) {
this = extend(Person(age=age), "PerDog",
.wt=wt
)
this
})
3)
setConstructorS3("PerDog", function(age=NA,wt=NA,dog_age=NULL) {
this = extend(Dog(dog_age=dog_age), "PerDog",
setConstructorS3("PerDog", function(age=NA,wt=NA,dog_age=NULL) {
extend(Person(age=age), "PerDog",
.wt=wt
)
})
)
this
})
Thanks,
ben
[[alternative HTML version deleted]]