尝试在'ordered'
类的基础上构建一个新类,使用R.version 3.3.1也试用了新的R-devel
R Under development (unstable) (2016-10-26 r71594) -- "Unsuffered Consequences"
我收到一个错误:
> setClass('newFactor', representation = c(tempValue = 'character'), contains = c('ordered'))
Error in validObject(.Object) :
invalid class “classRepresentation” object: invalid object for slot "slots" in class "classRepresentation": got class "character", should be or extend class "list"
>
同样适用于稳定版本3.2.5
编辑
好的,谢谢@hrbrmstr我明白R 3.3不支持在S4对象中使用S3对象,但我仍然感到困惑。
这:
numWithId <- setClass("numWithId", slots = c(id = "character"),contains = "numeric")
应该可行,但是:
numWithId <- setClass("numWithId", slots = c(id = "character"),contains = "factor")
不应该,除非它确实!
> numWithId <- setClass("numWithId", slots = c(id = "character"),contains = 'factor')
> new('numWithId')
An object of class "numWithId"
integer(0)
Slot "id":
character(0)
Slot "levels":
character(0)
Slot ".S3Class":
[1] "factor"
另一方面,这不起作用:
numWithId <- setClass("numWithId", slots = c(id = "character"),contains = 'ordered')
Error in makePrototypeFromClassDef(properties, ClassDef, immediate, where) :
in constructing the prototype for class “numWithId”: prototype has class “S4”, but the data part specifies class “integer”
当我将list
添加到contains =
时,它有效{我觉得这不是正确的做法}
> setClass("numWithId", slots = c(id = "character"),contains = c('list', 'ordered'))
> new("numWithId")
An object of class "numWithId"
list()
Slot "id":
character(0)
Slot ".S3Class":
[1] "ordered" "factor"
Slot "levels":
character(0)
>
将S3对象继承到S4对象的正确方法是什么?