class(base)
class()所属R语言包:base
Object Classes
对象类
译者:生物统计家园网 机器人LoveR
描述----------Description----------
R possesses a simple generic function mechanism which can be used for an object-oriented style of programming. Method dispatch takes place based on the class of the first argument to the generic function.
ŕ拥有一个面向对象的编程风格可以使用一个简单的通用功能的机制。方法调度类的第一个参数的通用功能的基础上发生。
用法----------Usage----------
class(x)
class(x) <- value
unclass(x)
inherits(x, what, which = FALSE)
oldClass(x)
oldClass(x) <- value
参数----------Arguments----------
参数:x
a R object
一个R对象
参数:what, value
a character vector naming classes. value can also be NULL.
字符向量命名类。 value也可以是NULL。
参数:which
logical affecting return value: see "Details".
逻辑影响返回值:见“详细资料”。
Details
详情----------Details----------
Many R objects have a class attribute, a character vector giving the names of the classes from which the object inherits. If the object does not have a class attribute, it has an implicit class, "matrix", "array" or the result of mode(x) (except that integer vectors have implicit class "integer"). (Functions oldClass and oldClass<- get and set the attribute, which can also be done directly.)
许多R对象有一个class属性,特征向量,从该对象继承的类的名称。如果该对象没有属性类,它有一个隐含的类,"matrix","array"或mode(x)的结果(除了整数向量隐类"integer") 。 (功能oldClass和oldClass<-get和set属性,也可直接做。)
When a generic function fun is applied to an object with class attribute c("first", "second"), the system searches for a function called fun.first and, if it finds it, applies it to the object. If no such function is found, a function called fun.second is tried. If no class name produces a suitable function, the function fun.default is used (if it exists). If there is no class attribute, the implicit class is tried, then the default method.
当一个泛型函数fun被应用到一个类的属性c("first", "second"),称为fun.first“,如果它发现它的功能的系统搜索,适用于它的对象的对象。被发现,如果没有这种功能一个名为fun.second的功能尝试。如果没有类名产生一个合适的功能,使用功能fun.default(如果存在的话)。如果没有阶级属性,试图隐类,则默认的方法。
The function class prints the vector of names of classes an object inherits from. Correspondingly, class<- sets the classes an object inherits from. Assigning a zero-length vector or NULL removes the class attribute.
的功能class打印矢量对象继承的类的名称。相应地,class<-设置一个对象继承的类。分配一个长度为零的向量或NULL删除类的属性。
unclass returns (a copy of) its argument with its class attribute removed. (It is not allowed for objects which cannot be copied, namely environments and external pointers.)
unclass回报(副本),其参数与它的阶级属性删除。 (这是不允许的,不能被复制,即环境和外部指针的对象。)
inherits indicates whether its first argument inherits from any of the classes specified in the what argument. If which is TRUE then an integer vector of the same length as what is returned. Each element indicates the position in the class(x) matched by the element of what; zero indicates no match. If which is FALSE then TRUE is returned by inherits if any of the names in what match with any class.
inherits指示是否任何从what参数指定的类继承它的第一个参数。如果which是TRUE然后what返回的相同长度的整数向量。每个元素表示class(x);零表示没有匹配的what元素匹配的位置。如果which是FALSE然后TRUE返回inherits如果what任何class比赛的任何名称。
All but inherits are primitive functions.
所有inherits是原始的功能。
正规类----------Formal classes----------
An additional mechanism of formal classes is available in packages methods which is attached by default. For objects which have a formal class, its name is returned by class as a character vector of length one. However, S3 method selection attempts to treat objects from an S4 class as if they had the appropriate S3 class attribute, as does inherits. Therefore, S3 methods can be defined for S4 classes. See Methods for details.
正规类的一个额外的机制,可以在包methods这是默认的重视。其中有一个正式的类的对象,它的名字返回class作为一个长度为1的特征向量。然而,S3方法的选择,试图从S4类治疗对象,如果他们有适当的S3类属性,inherits。因此,S3的方法可以定义为S4类。有关详情,请参阅方法。
The replacement version of the function sets the class to the value provided. For classes that have a formal definition, directly replacing the class this way is strongly deprecated. The expression as(object, value) is the way to coerce an object to a particular class.
更换版本的功能设置类提供的值。对于类,有一个正式的定义,直接更换强烈地反对这样的类。 as(object, value)是一个特定的类的对象的方式来要挟。
The analogue of inherits for formal classes is is. The two functions behave consistently objects with one exception: S4 classes can have conditional inheritance, with an explicit test. In this case, is will test the condition, but inherits ignores all conditional superclasses.
inherits正规类的模拟是is。这两个函数的行为始终有一个例外对象:S4类可以继承条件,一个明确的测试。在这种情况下,is将测试条件,但inherits忽略所有有条件的父。
注意----------Note----------
Functions oldClass and oldClass<- behave in the same way as functions of those names in S-PLUS 5/6, but in R UseMethod dispatches on the class as returned by class (with some interpolated classes: see the link) rather than oldClass. However, group generics dispatch on the oldClass for efficiency, and internal generics only dispatch on objects for which is.object is true.
功能oldClass和oldClass<-表现在5/6,S-PLUS的名称相同的功能的方式,但在RUseMethod类class返回调度 (有一些插值类:见链接),而不是oldClass。然而,仿制药组派遣oldClass效率,内部泛型只派遣is.object是真正的对象。
参见----------See Also----------
UseMethod, NextMethod, "group generic", "internal generic"
UseMethod,NextMethod,组通用的,内部通用的
举例----------Examples----------
x <- 10
class(x) # "numeric"[“数字”]
oldClass(x) # NULL[为NULL]
inherits(x, "a") #FALSE[假]
class(x) <- c("a", "b")
inherits(x,"a") #TRUE[真]
inherits(x, "a", TRUE) # 1[1]
inherits(x, c("a", "b", "c"), TRUE) # 1 2 0[1 2 0]
转载请注明:出自 生物统计家园网(http://www.biostatistic.net)。
注:
注1:为了方便大家学习,本文档为生物统计家园网机器人LoveR翻译而成,仅供个人R语言学习参考使用,生物统计家园保留版权。
注2:由于是机器人自动翻译,难免有不准确之处,使用时仔细对照中、英文内容进行反复理解,可以帮助R语言的学习。
注3:如遇到不准确之处,请在本贴的后面进行回帖,我们会逐渐进行修订。
|