R:将类定义为类的列表
我对classR:将类定义为类的列表,r,list,oop,R,List,Oop,我对classmultiset的定义如下: ### MULTISET ### setClass('multiset' ,slots=c( obj="character", amount="numeric")) # init multiset# createMultiset = function(object,val=numeric(0)){ mset = new('multiset',obj=object,amount=va
multiset
的定义如下:
### MULTISET ###
setClass('multiset'
,slots=c(
obj="character",
amount="numeric"))
# init multiset#
createMultiset = function(object,val=numeric(0)){
mset = new('multiset',obj=object,amount=val)
return(mset)
}
# example
m1 <- createMultiset('person',12)
我的定义的问题是它允许插入任何列表
new('multisets',objects=list('a',1))
如何限制
multiset
仅包含类为multiset
的对象列表?prototype为表示中指定的插槽提供默认数据<代码>原型功能用于设置默认数据
setClass('multiset',
representation = representation(obj = "character", amount = "numeric"),
prototype = prototype(obj = 'hi', amount = 0))
getClass('multiset')
# Class "multiset" [in ".GlobalEnv"]
#
# Slots:
#
# Name: obj amount
# Class: character numeric
new("multiset")
# An object of class "multiset"
# Slot "obj":
# [1] "hi"
#
# Slot "amount":
# [1] 0
# init multiset#
setMethod(f = 'initialize',
signature = 'multiset',
definition = function(.Object, ..., amount = numeric(0)){
print('you just initialized the class - multiset')
callNextMethod(.Object, ..., amount = amount)
})
new('multiset', amount = 2)
# [1] "you just initialized the class - multiset"
# An object of class "multiset"
# Slot "obj":
# [1] "hi"
#
# Slot "amount":
# [1] 2
setClass('multisets',
contains = 'multiset',
representation = representation(objects = 'list'),
prototype = prototype(objects = list('bye')))
new('multisets', objects = list('a',1))
# An object of class "multisets"
# Slot "objects":
# $obj
# [1] "a"
#
# $amount
# [1] 1
#
#
# Slot "obj":
# [1] "hi"
#
# Slot "amount":
# [1] 0
# define class SS
setClass('SS', representation = representation(money = 'numeric'))
# define class DB which is a subclass of both multiset and SS
setClass('DB', contains = (c('SS', 'multiset')))
a1 <- new('multiset', obj = 'hello')
b1 <- new('SS', money = 50)
new('DB', a1, b1)
# [1] "you just initialized the class - multiset"
# An object of class "DB"
# Slot "money":
# [1] 50
#
# Slot "obj":
# [1] "hello"
#
# Slot "amount":
# numeric(0)
setClass('multiset',
表示=表示(obj=“character”,amount=“numeric”),
原型=原型(obj=‘高’,金额=0))
getClass('multiset')
#类“multiset”[in.GlobalEnv”]
#
#插槽:
#
#名称:obj金额
#类别:字符数字
新(“多集”)
#“multiset”类的对象
#插槽“obj”:
#[1]“你好”
#
#插槽“金额”:
# [1] 0
#初始多集#
setMethod(f='initialize',
签名='multiset',
定义=函数(.Object,…,amount=数值(0)){
print('您刚刚初始化了类-multiset')
callNextMethod(.Object,…,amount=amount)
})
新('multiset',金额=2)
#[1]“您刚刚初始化了类-multiset”
#“multiset”类的对象
#插槽“obj”:
#[1]“你好”
#
#插槽“金额”:
# [1] 2
setClass('多集',
包含='multiset',
表示=表示(对象=‘列表’),
原型=原型(对象=列表('bye'))
新建('multiset',objects=list('a',1))
#“多集”类的对象
#插槽“对象”:
#$obj
#[1]“a”
#
#美元金额
# [1] 1
#
#
#插槽“obj”:
#[1]“你好”
#
#插槽“金额”:
# [1] 0
#定义类SS
setClass('SS',表示法=表示法(货币='numeric'))
#定义类DB,它是multiset和class的子类
setClass('DB',contains=(c('SS','multiset'))
a1
setClass('multiset',
representation = representation(obj = "character", amount = "numeric"),
prototype = prototype(obj = 'hi', amount = 0))
getClass('multiset')
# Class "multiset" [in ".GlobalEnv"]
#
# Slots:
#
# Name: obj amount
# Class: character numeric
new("multiset")
# An object of class "multiset"
# Slot "obj":
# [1] "hi"
#
# Slot "amount":
# [1] 0
# init multiset#
setMethod(f = 'initialize',
signature = 'multiset',
definition = function(.Object, ..., amount = numeric(0)){
print('you just initialized the class - multiset')
callNextMethod(.Object, ..., amount = amount)
})
new('multiset', amount = 2)
# [1] "you just initialized the class - multiset"
# An object of class "multiset"
# Slot "obj":
# [1] "hi"
#
# Slot "amount":
# [1] 2
setClass('multisets',
contains = 'multiset',
representation = representation(objects = 'list'),
prototype = prototype(objects = list('bye')))
new('multisets', objects = list('a',1))
# An object of class "multisets"
# Slot "objects":
# $obj
# [1] "a"
#
# $amount
# [1] 1
#
#
# Slot "obj":
# [1] "hi"
#
# Slot "amount":
# [1] 0
# define class SS
setClass('SS', representation = representation(money = 'numeric'))
# define class DB which is a subclass of both multiset and SS
setClass('DB', contains = (c('SS', 'multiset')))
a1 <- new('multiset', obj = 'hello')
b1 <- new('SS', money = 50)
new('DB', a1, b1)
# [1] "you just initialized the class - multiset"
# An object of class "DB"
# Slot "money":
# [1] 50
#
# Slot "obj":
# [1] "hello"
#
# Slot "amount":
# numeric(0)