美文网首页
R代码:面向对象之s3和s4

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

作者: lengol | 来源:发表于2016-03-15 21:58 被阅读164次

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

      本文链接:https://www.haomeiwen.com/subject/kadqlttx.html