Scala中的模式匹配与隐式参数
1. 密封类层次结构与详尽匹配
在Scala中,密封类层次结构可以实现详尽的模式匹配。例如,我们定义一个密封的抽象基类 HttpMethod 来表示HTTP允许的消息类型或“方法”:
sealed abstract class HttpMethod() {
def body: String
def bodyLength = body.length
}
case class Connect(body: String) extends HttpMethod
case class Delete (body: String) extends HttpMethod
case class Get (body: String) extends HttpMethod
case class Head (body: String) extends HttpMethod
case class Options(body: String) extends HttpMethod
case class Post (body: String) extends HttpMethod
case class Put (body: String) extends HttpMethod
case class Trace (body: String) extends HttpMethod
def handle (method: HttpMethod) = method match {
case Connect (body) => s"connect: (length: $
超级会员免费看
订阅专栏 解锁全文

被折叠的 条评论
为什么被折叠?



