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

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

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

                                         Generate an Object from a Class
                                         生成类的对象

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

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

Given the name or the definition of a class, plus optionally data to be included in the object, new returns an object from that class.
鉴于一类的名称或定义,加上可选的数据对象包括,new返回一个类的对象。


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


new(Class, ...)

initialize(.Object, ...)



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

参数:Class
either the name of a class, a character string, (the usual case) or the object describing the class (e.g., the value returned by getClass).
无论是一个类,一个character字符串,(通常情况下)或描述类的对象的名称(例如,返回的值getClass)。


参数:...
data to include in the new object.  Named arguments correspond to slots in the class definition. Unnamed arguments must be objects from classes that this class extends.
数据包括在新的对象。命名参数在类定义中的对应插槽。未命名的参数必须是对象的类,这个类扩展。


参数:.Object
An object:  see the Details section.
对象:见“详细资料”节。


Details

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

The function new begins by copying the prototype object from the class definition.  Then information is inserted according to the ... arguments, if any.  As of version 2.4 of R, the type of the prototype object, and therefore of all objects returned by new(), is "S4" except for classes that extend one of the basic types, where the prototype has that basic type.  User functions that depend on typeof(object) should be careful to handle "S4" as a possible type.
开始复制的原型对象的类定义的功能new。然后信息被插入到...参数,如果有的话。至于研发,原型对象的类型,版本2.4,因此所有的对象返回new(),"S4"类扩展的基本类型之一,原型,基本除外类型。用户功能依赖typeof(object)的应该小心处理"S4"作为一个可能的类型。

Note that the name of the first argument, "Class" entails that "Class" is an undesirable slot name in any formal class: new("myClass", Class = <value>) will not work.
请注意,第一个参数的名称,"Class"需要"Class"是一个在任何正式的类不良插槽名称:new("myClass", Class = <value>)不会工作。

The interpretation of the ... arguments can be specialized to particular classes, if an appropriate method has been defined for the generic function "initialize".  The new function calls initialize with the object generated from the prototype as the .Object argument to initialize.
...参数的解释可以是专门为特定的类,如果已定义泛型函数"initialize"适当的方法。 new函数调用initialize从原型生成.Objectinitialize参数的对象。

By default, unnamed arguments in the ... are interpreted as objects from a superclass, and named arguments are interpreted as objects to be assigned into the correspondingly named slots.  Thus, explicit slots override inherited information for the same slot, regardless of the order in which the arguments appear.
默认情况下,在未命名的参数...被解释为从一个父对象,被解释的对象被分配到相应的命名槽和命名参数。因此,明确插槽重写继承的相同插槽的信息,无论参数出现的顺序。

The initialize methods do not have to have ... as their second argument (see the examples).  Initialize methods are often written when the natural parameters describing the new object are not the names of the slots.  If you do define such a method, note the implications for future subclasses of your class.  If these have additional slots, and your initialize method has ... as a formal argument, then your method should pass such arguments along via callNextMethod.  If your method does not have this argument, then either a subclass must have its own method or else the added slots must be specified by users in some way other than as arguments to new.
没有initialize方法有...作为他们的第二个参数(见例子)。初始化的方法往往是写时,描述新对象的自然参数不插槽的名称。如果你定义了这样一个方法,注意未来您的类的子类的影响。如果这些有额外的插槽,和你的initialize方法...作为一个正式的说法,那么你的方法应该通过callNextMethod传递这样的论点。如果你的方法没有这样的说法,然后一个子类必须有它自己的方法或其他增加的插槽必须被指定在用户以外的其他一些方法作为new参数。

For examples of initialize methods, see initialize-methods for existing methods for classes "traceable" and "environment", among others. See the comments there on subclasses of "environment"; any initialize methods for these should be sure to allocate a new environment.
对于initialize方法的例子,请参阅initialize-methods类"traceable"和"environment"等等,现有的方法。看到有子类的意见"environment"任何initialize这些方法应该是确保分配一个新的环境。

Methods for initialize can be inherited only by simple inheritance, since it is a requirement that the method return an object from the target class.  See the simpleInheritanceOnly argument to setGeneric and the discussion in setIs for the general concept.
initialize方法可以继承的,只能通过简单的继承,因为它是一个要求,该方法返回的目标类对象。看到simpleInheritanceOnly参数setGeneric和setIs一般概念的讨论。

Note that the basic vector classes, "numeric", etc. are implicitly defined, so one can use new for these classes.
请注意基本向量类,"numeric"等等都隐含定义,所以可以使用new这些类。


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

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

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

Classes for an overview of defining class, and
类的定义类的概述,


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


## using the definition of class "track" from \link{setClass}[#使用类的“轨道”的定义,从\链接{setClass}]



## a new object with two slots specified[#指定的两个插槽的新对象]
t1 <- new("track", x = seq_along(ydata), y = ydata)

# a new object including an object from a superclass, plus a slot[一个新的对象包括从超类的对象,加上一个插槽]
t2 <- new("trackCurve", t1, smooth = ysmooth)

### define a method for initialize, to ensure that new objects have[#定义一个initialize方法,以确保新的对象有]
### equal-length x and y slots.[#长度相等的x和y插槽。]

setMethod("initialize",
          "track",
          function(.Object, x = numeric(0), y = numeric(0)) {
            if(nargs() > 1) {
              if(length(x) != length(y))
                stop("specified x and y of different lengths")
              .Object@x <- x
              .Object@y <- y
            }
            .Object
          })

### the next example will cause an error (x will be numeric(0)),[##下面的例子将导致错误(x是数字(0)),]
### because we didn't build in defaults for x,[#因为我们没有建立在默认为x,]
### although we could with a more elaborate method for initialize[#虽然我们可以更详细的初始化方法]

try(new("track", y = sort(stats::rnorm(10))))

## a better way to implement the previous initialize method.[#一个更好的办法实施前的initialize方法。]
## Why?  By using callNextMethod to call the default initialize method[#为什么?使用callNextMethod调用默认的initialize方法]
## we don't inhibit classes that extend "track" from using the general[#我们不抑制类,延长使用一般的“轨道”]
## form of the new() function.  In the previous version, they could only[#形成新的()函数。在以前的版本,他们只能]
## use x and y as arguments to new, unless they wrote their own[#使用x和y作为新的参数,除非他们写自己的]
## initialize method.[#初始化方法。]

setMethod("initialize", "track", function(.Object, ...) {
    .Object <- callNextMethod()
    if(length(.Object@x) != length(.Object@y))
     stop("specified x and y of different lengths")
    .Object
  })


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


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

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-1-24 21:08 , Processed in 0.019888 second(s), 16 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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