6
我有以下代碼:是否可以鏈接不同特徵的方法?
class Parameterizable{
var map: Map[String, String] = new scala.collection.immutable.HashMap()
def put(entry: Tuple2[String, String]) = {
map = map + entry; this
}
}
class Query() extends Parameterizable{
override def toString = {
map.isEmpty match{
case true => ""
case false => "?" + map.map{case (key, value) => key + "=" + value}.mkString("&")
}
}
}
trait PageParameter extends Parameterizable{
def page(page: Int) = put(("page" -> page.toString))
def pageSize(pageSize: Int) = put(("pagesize" -> pageSize.toString))
}
trait DateParameter extends Parameterizable{
def fromDate(date: java.util.Date) = put(("fromdate" -> (date.getTime()/1000L).toString()))
def toDate(date: java.util.Date) = put(("todate" -> (date.getTime()/1000L).toString()))
}
//and other similar traits
我想這樣做:
class ExtendedQuery extends Query with PageParameter with DateParameter
val query = new ExtendedQuery
query.page(4).pageSize(5).fromDate(new java.util.Date)
或:
query.and().page(4).and().pageSize(5).and().fromDate(new java.util.Date)
是否有可能在Scala呢?