R代码:面向对象之s3和s4

s3 面向对象

Person <- function(name){
slot = list(name = name)
class(slot) = "Person"
print.Person <- function(x){
cat("我叫", x$name)
}
slot
}
Person("张大饼子")

===============================

s4 面向对象

setClass("person",
slots=c(name="character",
age="numeric"))
setGeneric("speak",
function(x)
standardGeneric("speak"))
setMethod(speak,
signature="person",
definition=function(object)
cat("我叫",object@name,"今年",object@age))
p <- new("person",name="张大饼子",age=18)
speak(p)

你可能感兴趣的:(R代码:面向对象之s3和s4)