找回密码
 注册
查看: 1857|回复: 0

R语言:NextMethod()函数中文帮助文档(中英文对照)

[复制链接]
发表于 2012-2-16 21:23:59 | 显示全部楼层 |阅读模式
NextMethod(methods)
NextMethod()所属R语言包:methods

                                        Call an Inherited Method
                                         调用继承的方法

                                         译者:生物统计家园网 机器人LoveR

描述----------Description----------

A call to callNextMethod can only appear inside a method definition.  It then results in a call to the first inherited method after the current method, with the arguments to the current method passed down to the next method.  The value of that method call is the value of callNextMethod.
一个callNextMethod调用只能出现在一个方法的定义。然后,它导致在调用当前方法的方法,先继承后,与目前的方法向下传递到下一个方法的参数。该方法调用的值是值callNextMethod。


用法----------Usage----------


callNextMethod(...)



参数----------Arguments----------

参数:...
Optionally, the arguments to the function in its next call (but note that the dispatch is as in the detailed description below; the arguments have no effect on selecting the next method.)  If no arguments are included in the call to callNextMethod, the effect is to call the method with the current arguments. See the detailed description for what this really means.  Calling with no arguments is often the natural way to use callNextMethod; see the examples.  
或者,在其下一次调用的函数的参数(但要注意,调度是在下面的详细描述;参数没有选择下一个方法的效果。)如果没有参数都包含在调用callNextMethod ,效果是与当前参数的方法调用。看到这究竟是怎样的详细描述。调用不带参数使用callNextMethod;看到的例子,往往是自然的方式。


Details

详情----------Details----------

The "next" method (i.e., the first inherited method) is defined to be that method which would have been called if the current method did not exist. This is more-or-less literally what happens: The current method (to be precise, the method with signature given by the defined slot of the method from which callNextMethod is called) is deleted from a copy of the methods for the current generic, and selectMethod is called to find the next method (the result is cached in a special object, so the search only typically happens once per session per combination of argument classes).
“下一步”的方法(即,首先继承的方法)的定义是,它会被称为如果当前方法不存在的方法。这是更多或较少从字面上发生了什么:从副本中删除目前的方法(要精确,与签名的方法从defined被称为方法的槽callNextMethod)目前通用的,selectMethod被称为寻找下一个方法(缓存在一个特殊的对象,结果是这样的搜索通常只发生一次每参数类的组合会议)的方法。

Note that the preceding definition means that the next method is defined uniquely when setMethod inserts the method containing the callNextMethod call, given the definitions of the classes in the signature. The choice does not depend on the path that gets us to that method (for example, through inheritance or from another callNextMethod call). This definition was not enforced in versions of R prior to 2.3.0, where the method was selected based on the target signature, and so could vary depending on the actual arguments.
请注意前面的定义,是指未来的唯一方法被定义时setMethod插入方法包含callNextMethod电话,在签名的类的定义。选择不依赖于路径可以让我们以这种方法(例如,通过继承或从另一个callNextMethod呼叫)。这个定义是不执行之前的R版本2.3.0,方法是选择目标签名的基础上,因此可能会有所不同根据实际参数。

It is also legal, and often useful, for the method called by callNextMethod to itself have a call to callNextMethod. This generally works as you would expect, but for completeness be aware that it is possible to have ambiguous inheritance in the S structure, in the sense that the same two classes can appear as superclasses in the opposite order in two other class definitions.  In this case the effect of a nested instance of callNextMethod is not well defined.  Such inconsistent class hierarchies are both rare and nearly always the result of bad design, but they are possible, and currently undetected.
这也是合法的,而且往往有用,callNextMethod本身有一个调用callNextMethod调用的方法。这通常你所期望的,但要注意,它有可能在这个意义上,相同的两个类可以作为父在相反的顺序出现在其他两个类定义,有S结构的暧昧继承的完整性。在这种情况下嵌套实例效果的callNextMethod没有很好地界定。这种不一致的类层次结构都是罕见的,几乎总是不好的设计结果,但他们是可能的,而且目前未被发现。

The statement that the method is called with the current arguments is more precisely as follows.  Arguments that were missing in the current call are still missing (remember that "missing" is a valid class in a method signature).  For a formal argument, say x, that appears in the original call, there is a corresponding argument in the next method call equivalent to x = x.  In effect, this means that the next method sees the same actual arguments, but arguments are evaluated only once.
更精确的说法是,该方法是与当前参数称为如下。在当前通话中缺少的论点是人仍下落不明(记得"missing"是一个有效的方法签名类)。一个正式的说法,说x,出现在原来的呼叫,有相应的参数,在未来的方法称之为等效x = x。实际上,这意味着,未来的方法看到了相同的实际参数,但参数是只计算一次。


值----------Value----------

The value returned by the selected method.
所选方法的返回值。


参考文献----------References----------

Software for Data Analysis: Programming with R Springer.  (For the R version.)
Programming with Data Springer (For the original S4 version.)

参见----------See Also----------

callGeneric to call the generic function with the current dispatch rules (typically for a group generic function); Methods for the general behavior of method dispatch.
callGeneric调用泛型函数与目前的调度规则(通常为一组通用功能);方法调度的一般行为的方法。


举例----------Examples----------



## some class definitions with simple inheritance[#类定义一些简单的继承]
setClass("B0" , representation(b0 = "numeric"))

setClass("B1", representation(b1 = "character"), contains = "B0")

setClass("B2", representation(b2 = "logical"), contains = "B1")

## and a rather silly function to illustrate callNextMethod[#和一个相当愚蠢的函数说明callNextMethod]

f <- function(x) class(x)

setMethod("f", "B0", function(x) c(x@b0^2, callNextMethod()))
setMethod("f", "B1", function(x) c(paste(x@b1,":"), callNextMethod()))
setMethod("f", "B2", function(x) c(x@b2, callNextMethod()))

b1 <- new("B1", b0 = 2, b1 = "Testing")

b2 <- new("B2", b2 = FALSE, b1 = "More testing", b0 = 10)

f(b2)
stopifnot(identical(f(b2), c(b2@b2, paste(b2@b1,":"), b2@b0^2, "B2")))

f(b1)

## a sneakier method: the *changed* x is used:[#sneakier方法:*改变* X用于:]
setMethod("f", "B2",
          function(x) {x@b0 <- 111; c(x@b2, callNextMethod())})
f(b2)
stopifnot(identical(f(b2), c(b2@b2, paste(b2@b1,":"), 111^2, "B2")))




转载请注明:出自 生物统计家园网(http://www.biostatistic.net)。


注:
注1:为了方便大家学习,本文档为生物统计家园网机器人LoveR翻译而成,仅供个人R语言学习参考使用,生物统计家园保留版权。
注2:由于是机器人自动翻译,难免有不准确之处,使用时仔细对照中、英文内容进行反复理解,可以帮助R语言的学习。
注3:如遇到不准确之处,请在本贴的后面进行回帖,我们会逐渐进行修订。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|小黑屋|生物统计家园 网站价格

GMT+8, 2025-1-23 22:34 , Processed in 0.022148 second(s), 15 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表