[R] Methods dispatch and inheritance R.oo

Henrik Bengtsson hb at stat.berkeley.edu
Thu Nov 6 06:54:16 CET 2008


Hi,

if you instead do:

setConstructorS3("ClassB", function(B=15) {
  extend(ClassA(), "ClassB",
    .size2 = B
  );
})

then you'll get:

> objB <- ClassB();
> objB
[1] "Class B: 15"


What was wrong with your setup?
What you ended up doing was to "extend" an object of class 'character'
(with value "ClassA").  With above fix, you now extend an object of
class 'ClassA' (which in turn extend 'Object').

Hope this help

/Henrik

On Wed, Nov 5, 2008 at 2:18 AM, Yuri Volchik <yuri.volchik at gmail.com> wrote:
>
> Hi to all members, i have a question about class inheritance and methods
> using R.oo package:
> I have the following code and it doesn't work, guess i'm doing smth wrong
> and there is nothing in the help.
>
> library(R.oo)
>
> setConstructorS3("ClassA", function(A=15) {
>  extend(Object(), "ClassA",
>    .size = A
>  );
> })
>
>
> setMethodS3("print", "ClassA", function(this,...) {
>  print(paste('Class A:',this$.size));
> })
>
>
> objA<-ClassA();
> objA
>
> [1] "Class A: 15"
>
> setConstructorS3("ClassB", function(B=15) {
>  extend("ClassA", "ClassB",
>    .size2 = B
>  );
> })
>
>
> setMethodS3("print", "ClassB", function(this,...) {
>  print(paste('Class B:',this$.size2));
> })
>
>
> objB<-ClassB();
> objB
>
> Error in this$.size2 : $ operator is invalid for atomic vectors
>
>
> --
> View this message in context: http://www.nabble.com/Methods-dispatch-and-inheritance-R.oo-tp20339090p20339090.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list