大家好,我在SCALA上创建了以下案例类:
sealed abstract class Value;
case class U(name: String) extends Value
case class L(name: String) extends Value
case class B(name: String) extends Value
sealed abstract class Term
case class Var(name: String) extends Term //variable name
case class Val(value: Value) extends Term //value
sealed abstract class Pattern //patterns
case class BGP(subject: Term, predicate: Term, obj: Term) extends Pattern
case class And( pat1: Pattern, pat2: Pattern) extends Pattern
case class Filter(pred: Predicate, pattern: Pattern ) extends Pattern
def function(p: Pattern): Unit = p match {
case BGP(Var(x), Val(y), Val(z)) => {
val con:conv = new conv()
val valor:Value = Val(y).value
}
然后,正如您所看到的,BGP包含Term并扩展到pattern,Val包含Values并扩展到Term,而U,L,B包含Strings并扩展为Value, 在我的函数中我想访问包含U或L或B案例类的字符串,变量valor = Val(y).value包含一个U类,例如,当我写valor.XXXX时不会出现我名称选项。最大的问题是如何从U访问String名称?
答案 0 :(得分:2)
您只需在Value
上定义,即btw可能是trait
。
sealed trait Value {
def name: String
}
case class U(name: String) extends Value
case class L(name: String) extends Value
case class B(name: String) extends Value