[R] The object argument of NextMethod.

ronggui ronggui.huang at gmail.com
Fri Apr 14 16:54:53 CEST 2006


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




More information about the R-help mailing list