R语言中,简单的S3和S4类的定义

发布时间:2020-03-06 13:59:12 作者:h2appy
来源:网络 阅读:3916

R语言中,简单的S3和S4类的定义


# 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(11, "jack", "male")

#print(st)

st

# S4 class

setClass("student",

         representation(

           id = "numeric",

           name = "character",

           sex = "character"

         ))

# print is not a S4 generic. show methods are mapped to print for convenience, though. 

setMethod("show", "student",

          function(object){

            cat(object@id, "\n")

            cat(object@name, "\n")

            cat(object@sex, "\n")

          })

st = new("student", id = 41, name = "tom",  sex = "male")

#print(st)

st


推荐阅读:
  1. Python 之 类的定义与简单使用
  2. java中类的定义

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

class r s3

上一篇:python多线程开发

下一篇:Egret之Eui.Button换肤

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》