通過哈德利韋翰的S4維基展望: https://github.com/hadley/devtools/wiki/S4S4構造函數和原型
setClass("Person", representation(name = "character", age = "numeric"),
prototype(name = NA_character_, age = NA_real_))
hadley <- new("Person", name = "Hadley")
我們如何可以設計的人一個構造函數(像這樣)
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"
更新:setClass返回一個默認的構造函數: 人< - SetClass( 「人」,...) – ctbrown