Hi, On Thu, Nov 6, 2008 at 2:12 AM, Yuri Volchik <[EMAIL PROTECTED]> wrote: > > Thanks for reply Henrik, seems obvious now. > Can child class (B) access argument of the parent class, i.e. can i rewrite > definition of the class B as > > setConstructorS3("ClassB", function() { > extend(ClassA(), "ClassB", > .size2 = A > ); > }) > > it didn't work for me, so guess i'm doing smth wrong and i couldn't find any > reference on inheritance in the help files.
Note that 'A' is only an argument (~= local variable) of the constructor function ClassA(). There is no way the (constructor) function ClassB() can know about that one; compare to: foo <- function(A=1) { } bar <- function(B=2) { # Inside here you have no access to 'A' in foo(), regardless if you call foo() or not. } So, I'm not sure what you are trying to do in reality. Note, since the value of 'A' is already assigned in ClassA() and part of its returned object, then it will also be part of the returned object from ClassB(), so you don't really have to assign 'A' again. I think this is a better example. See if you want to achive what ClassB or ClassC do: setConstructorS3("ClassA", function(A=0) { extend(Object(), "ClassA", A = A ); }) > objA <- ClassA(); > ll(objA) member data.class dimension objectSize 1 A numeric 1 32 > objA$A [1] 0 setConstructorS3("ClassB", function(A=0, B=1) { extend(ClassA(A=A), "ClassB", B = B ); }) > objB <- ClassB(A=1, B=2); > ll(objB); member data.class dimension objectSize 1 A numeric 1 32 2 B numeric 1 32 > objB$A [1] 1 > objB$B [1] 2 If you really want to access a field from a superclass inside the constructor function, you have to first get the object returned by the constructor of the superclass, and then from that object retrieve the field: setConstructorS3("ClassC", function(B=1) { this <- extend(ClassA(), "ClassC", B = B ); this$copyOfA <- this$A; this; }) > objC <- ClassC(B=3); > ll(objC); member data.class dimension objectSize 1 A numeric 1 32 2 B numeric 1 32 > objC$A [1] 0 > objC$B [1] 3 Note, there is nothing "magic" going on inside Object(), extend() or any other R.oo methods. Most of the things you can think of as regular lists and functions. The new think is that objects inheriting from the Object class store their values inside an environment, which is why the object behaves as it is passed by reference. Hope this helps /Henrik > > -- > View this message in context: > http://www.nabble.com/Methods-dispatch-and-inheritance-R.oo-tp20339090p20358341.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@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. > ______________________________________________ R-help@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.