【发布时间】:2011-11-28 21:15:03
【问题描述】:
我在包 R.methodsS3 中使用 setMethodS3 来创建 S3 方法。假设我有两个类,class Parent 和 class Child(R.oo 对象)。 class Child 继承自 class Parent。两者都有方法MyMethod()。我如何从孩子的MyMethod() 调用超类MyMethod()(父母的MyMethod)?我试过这个$MyMethod(),但是它调用了Child的MyMethod()
这是一个简化的例子:
library( R.oo )
setConstructorS3( "Parent" , definition =
function()
{
extend( Object() , "Parent" , .stateVar1 = FALSE )
} )
setMethodS3( "MyMethod" , "Parent" , appendVarArgs = FALSE , definition =
function( this , someParam , ... )
{
print( this$.stateVar1 )
print( someParam )
} )
setConstructorS3( "Child" , definition =
function()
{
extend( Parent() , "Child" )
} )
setMethodS3( "MyMethod" , "Child" , appendVarArgs = FALSE , definition =
function( this , someParam , ... )
{
NextMethod( "MyMethod" ) # does not work
this$MyMethod( someParam ) # also does not work
} )
child = Child()
child$MyMethod()
【问题讨论】:
-
感谢 Henrik 提供了我们在电子邮件中讨论的解决方法:MyMethod.Parent(this, someParam)