-
Notifications
You must be signed in to change notification settings - Fork 9
数据模型
Song Kun edited this page Jan 3, 2018
·
1 revision
将使用 and
和 or
表达的 data model 转化为 Scala 代码。
第一种模式是:A has a B and C
,可以用 case class
或者 trait
对其建模:
case class A(b: B, c: C)
trait A {
def b: B
def c: C
}
第二种模式为:A is a B or C
,使用 sealed trait
+ final case class
对其建模:
sealed trait A
final case class B() extends A
final case class C() extends A
代数数据类型是使用以上两种模式(product type + sum type)的任意数据。