浏览 Hadley Wickham 的 S4 wiki: https ://github.com/hadley/devtools/wiki/S4
setClass("Person", representation(name = "character", age = "numeric"),
prototype(name = NA_character_, age = NA_real_))
hadley <- new("Person", name = "Hadley")
我们如何为 Person 设计一个构造函数(像这样)
Person<-function(name=NA,age=NA){
new("Person",name=name,age=age)
}
不这样做:
> Person()
Error in validObject(.Object) :
invalid class "Person" object: 1: invalid object for slot "name" in class "Person": got class "logical", should be or extend class "character"
invalid class "Person" object: 2: invalid object for slot "age" in class "Person": got class "logical", should be or extend class "numeric"