The definition of simple S3 and S4 classes in the R language
# S3 Class
Newstudent <-function (SID, Sname, Ssex) {
TMP <-list (id = SID, name = sname, sex = ssex)
Class (TMP) <-"Student"
Return (TMP)
}
Print.student <-Function (ST) {
Cat (st$id, "\ n")
Cat (St$name, "\ n")
Cat (St$sex, "\ n")
}
St = Newstudent (one, "Jack", "male")
#print (ST)
St
# S4 Class
SetClass ("Student",
Representation (
id = "Numeric",
name = "character",
Sex = "character"
))
# Print is not a S4 generic. Show methods is mapped to print for convenience, though.
Setmethod ("Show", "Student",
Function (object) {
Cat ([email protected], "\ n")
Cat ([email protected], "\ n")
Cat ([email protected], "\ n")
})
st = new ("student", id = $, name = "Tom", sex = "male")
#print (ST)
St
This article is from the "GONE with the Wind" blog, please be sure to keep this source http://h2appy.blog.51cto.com/609721/1855788
The definition of simple S3 and S4 classes in the R language