arrays - 如何将数组/列表添加到对象

标签 arrays r list oop

我创建了一个学生类:

setClass("student", slots=list(name="character", age="numeric", GPA="numeric"))

现在我想创建另一个类:

setClass("classRoom", slots=list(id = "numeric"),contains = "student")

唯一的问题是我想添加一个学生列表而不是一个学生。

我如何实现这一目标?

最佳答案

我猜你已经实现了你想要的。 在 classRoom 中添加 student 类将可以访问 student 类的所有属性。您现在可以使用类(class)学生的属性在 classRoom 中输入新学生。

# define a class called student
setClass("student", slots=list(name="character", age="numeric", GPA="numeric"))

# inherit from student
setClass("classRoom", slots=list(id = "numeric"),contains = "student")

# Now create new objects of classroom:
> s1 <- new("classRoom",name="John", age=21, GPA=3.5, id=001)
> s2 <- new("classRoom",name="Grisham", age=24, GPA=345, id=002)
> s3 <- new("classRoom",name="Flamingo", age=22, GPA=3.8, id=003)

像这样,你已经在你的教室里添加了学生。

关于arrays - 如何将数组/列表添加到对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44692063/

相关文章:

r - 调整 geom_tile 大小以消除边距效应

Python:如果字符串包含特定关键字,则从列表中删除特定字符串 v.2

list - orgmode,引用编号列表中的项目

arrays - 谷歌表 : How to find the length of array?

ios - 如何将自定义结构数组保存到 plist swift

R CMD 检查注释 : Found no calls to: ‘R_registerRoutines’ , ‘R_useDynamicSymbols’

r - 如何将字符串转换为聚合分位数?

list - 从列表中选择递增序列

c - 通过在 C 中分配整数数组来理解这种行为

javascript - 将3个对象转为多维数组