首页 文章

如何从Scala的案例类中读取?

提问于
浏览
-2

我是Scala的新手 . 我有一个案例类 . 代码如下 .

case class ReportInfoPosted(
  name:  Option[String],
  id:    Option[String],
  order: Option[Int]
)

我还有一个函数返回类的 seq 对象 . 这就是返回的内容 .

ReportInfoPosted(Some(Sales Dollars),Some(4e6d8ec1-4c00-4193-be15-2fa0509849a7),Some(0))

现在我想从对象中读取值 . 我查看了网上的一些资源,这是我尝试过的 .

for(el <- reportlist){
    println(el.input)
}

for(el <- reportlist){
    println(el.id)
}

顺便说一句, reportlistseq 的主题 . 没有人工作 . 我不知道该怎么办 .

1 回答

  • 2

    你的问题很模糊 . 你的意思是这个吗?

    val a = ReportInfoPosted(Some("a"), Some("a"), Some(1))
    val b = ReportInfoPosted(Some("b"), Some("b"), Some(2))
    val reportlist: Seq[ReportInfoPosted] = Seq(a,b)
    
    for (report <- reportlist) {
      println(report.name)
    }
    

    打印:

    Some(a)
    Some(b)
    

相关问题