在 06-4-14,Gabor Grothendieck<[EMAIL PROTECTED]> 写道:
> In section 5.5 of the language manual it says:
>
> "It is important to realize that the choice of the next method depends on the
> current values of .Generic and .Class and not on the object. So changing
> the object in a call to NextMethod affects the arguments received by
> the next method but does not affect the choice of the next method."

Can anyone give an example to demostrated "affects the arguments received by
 the next method"?In my example NextMethod("foo") and
NextMethod("foo",x) give the same result too.

> foo=function(x)  {UseMethod("foo")}
>
> foo.cls1=function(x)
+ {
+  x=x+1
+  NextMethod("foo")
+ }
>
> foo.ncls=function(x)
+ {
+ cat("ncls\n")
+ }
>
> foo.cls2=function(x)
+ {
+ cat("cls2\n");print(x)
+ }
>
> a=1;class(a)=c("cls1","cls2")
>
> foo(a)
cls2
[1] 2
attr(,"class")
[1] "cls1" "cls2"
> foo=function(x)  {UseMethod("foo")}
>
> foo.cls1=function(x)
+ {
+  x=x+1
+  NextMethod("foo",x)
+ }
>
> foo.ncls=function(x)
+ {
+ cat("ncls\n")
+ }
>
> foo.cls2=function(x)
+ {
+ cat("cls2\n");print(x)
+ }
>
> a=1;class(a)=c("cls1","cls2")
>
> foo(a)
cls2
[1] 2
attr(,"class")
[1] "cls1" "cls2"



> I think this needs to be put into ?NextMethod too since the current
> wording in ?NextMethod appears to contract the language manual:
>
>   "object: an object whose class will determine the method to be
>           dispatched.  Defaults to the first argument of the enclosing
>           function."
>
> At any rate, try using .Class to direct it to the appropriate method like 
> this:
>
> > foo <- function(x) UseMethod("foo")
> > foo.cls1 <- function(x) { x <- x+1; .Class <- class(x) <- "ncls"; 
> > NextMethod() }
> > foo.ncls <- function(x) cat("ncls\n")
> > foo.cls2=function(x) { cat("cls2\n"); print(x) }
> > a <- 1; class(a) <- c("cls1","cls2")
> >
> > foo(a)
> ncls
>
> On 4/14/06, ronggui <[EMAIL PROTECTED]> wrote:
> > My question is when the object argument of NexthMethod be used?
> >
> > In the following example, weather object argument is used will not
> > affects the result.
> >
> > ###
> > foo=function(x)  {UseMethod("foo")}
> >
> > foo.cls1=function(x)
> > {
> >  x=x+1;class(x)<-"ncls"
> >  NextMethod()
> > }
> >
> > foo.ncls=function(x)
> > {
> > cat("ncls\n")
> > }
> >
> > foo.cls2=function(x)
> > {
> > cat("cls2\n");print(x)
> > }
> >
> > a=1;class(a)=c("cls1","cls2")
> >
> > > foo(a)
> > cls2
> > [1] 2
> > attr(,"class")
> > [1] "ncls"
> >
> > ###
> > > foo=function(x)  {UseMethod("foo")}
> > >
> > > foo.cls1=function(x)
> > + {
> > +   x=x+1;class(x)<-"ncls"
> > +   NextMethod(,x)
> > + }
> > >
> > > foo.ncls=function(x)
> > + {
> > + cat("ncls\n")
> > + }
> > >
> > > foo.cls2=function(x)
> > + {
> > + cat("cls2\n");print(x)
> > + }
> > >
> > > a=1;class(a)=c("cls1","cls2")
> > >
> > > foo(a)
> > cls2
> > [1] 2
> > attr(,"class")
> > [1] "ncls"
> >
> > Thank you very much.
> >
> > --
> > 黄荣贵
> > Deparment of Sociology
> > Fudan University
> >
> >
> >
> > ______________________________________________
> > [email protected] mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide! 
> > http://www.R-project.org/posting-guide.html
> >
> >
>


--
黄荣贵
Deparment of Sociology
Fudan University

______________________________________________
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

Reply via email to