[R] class

schuster mail at friedrich-schuster.de
Fri Jul 23 18:42:40 CEST 2010


Hi, 

Strange, there seems to be different behavior of "old style" classes and S4 
classes.

This worked just like you expected (but it's not the same thing, no S4 
classes)

f2=function(x,...) UseMethod("fxy")
fxy.default=function(x,...){
	print("default")
}
fxy.X=function(x,...) {
	print("X")
	NextMethod()
}
fxy.Y=function(x,...) {
	print("Y")
	NextMethod()
}

x <- 1
y <- 2
class(x) <- c("X")
class(y) <- c("X", "Y")
f2(x)
f2(y)

Did you find a solution?

On Thursday 22 July 2010 10:23:37 am Yuan Jian wrote:
> Hello,
>> ###� I created two classes "A" and "B". "A" is the superclass of "B".
>> setClass("A", representation(s1="numeric"),prototype=prototype(s1=8))
> setClass("B",contains="A",representation(s2="character"),prototype=list(s2=
> "hi")) myA=new("A")
> myB=new("B")
>> ####� I created functions for "A" and "B"
>> f1=function(x,...) UseMethod("f")
> f.default=function(x,...){
> ��� print("default")
> �}
> �f.A=function(x,...) {
> ���� print(paste("x at s1=", x at s1, sep=""))
> ��� �NextMethod()
> �}
> �f.B=function(x,...) {
> ��� �print(paste("x at s2=", x at s2, sep=""))
> ���� NextMethod()
> �}
>> #### when I do
> �f1(myB)
> ## R gave me the result
> [1] "x at s2=hi"
> [1] "default"
> ## but I think the result should be
> [1] "x at s2=hi"
> [1] "x at s1=8"
> [1] "default"
>> ## because the the NextMethod() should go to f1.A not directly to
>  f1.default. could you please tell me where I got wrong understanding?
>> Yu
>> 
>> 
> 
> 
> 	[[alternative HTML version deleted]]
> 

-- 
----
Friedrich Schuster
Dompfaffenweg 6
69123 Heidelberg



More information about the R-help mailing list