可能标题不是很清楚。这是我的问题。
假设我有一个特征,它用一系列配置参数定义了一个应用程序。这些参数包含在 Map
中,其中一些具有默认值。
trait ConfApp {
val dbName: String
lazy val conf: scala.collection.mutable.Map[String, Any] = scala.collection.mutable.Map("db" -> dbName, "foo" -> "bar")
}
所以我可以按如下方式创建自定义应用程序:
class MyApp extends ConfApp {
override val dbName = "my_app_db"
// print app configuration parameters
println(conf)
def add() = {...}
...
}
val M1 = new Myapp // Map(db -> my_app_db, foo -> bar)
我想创建其他特征来设置一些其他配置参数。换句话说,我希望能够做类似的事情:
class MyApp2 extends ConfApp with LogEnabled {
override val dbName = "my_app2_db"
// print app configuration parameters
println(conf)
def add() = {...}
...
}
val M2 = new Myapp2 // Map(db -> my_app_db, foo -> bar, log -> true)
到目前为止,我已经设法做到了以下几点:
trait LogEnabled {
val conf: scala.collection.mutable.Map[String, Any]
conf("log") = true
}
trait LogDisabled {
val conf: scala.collection.mutable.Map[String, Any]
conf("log") = false
}
trait ConfApp {
val dbName: String
lazy val conf: scala.collection.mutable.Map[String, Any] = scala.collection.mutable.Map("db" -> dbName, "foo" -> "bar")
}
class MyApp extends ConfApp {
val dbName = "my_app_db"
println(conf)
}
class MyApp2 extends ConfApp with LogDisabled {
val dbName = "my_app_db"
println(conf)
}
val M = new MyApp // Map(db -> my_app_db, foo -> bar)
val M2 = new MyApp2 // Map(log -> false, foo -> bar, db -> null)
但正如您在 M2
中所见,db
参数为 null
。我不明白我做错了什么。
老实说,我一点也不喜欢这种使用可变 Map 的方法,但我还没有设法做得更好。
最佳答案
您仍然可以通过这种方式使用不可变的 Map
:
scala> trait ConfApp {
| val dbName: String
| def conf: Map[String, Any] = Map("db" -> dbName, "foo" -> "bar")
| }
defined trait ConfApp
scala> trait LogEnabled extends ConfApp {
| override def conf = super.conf.updated("log", true)
| }
defined trait LogEnabled
scala> trait LogDisabled extends ConfApp {
| override def conf = super.conf.updated("log", false)
| }
defined trait LogDisabled
scala> class MyApp extends ConfApp {
| val dbName = "my_app_db"
| println(conf)
| }
defined class MyApp
scala> class MyApp2 extends ConfApp with LogDisabled {
| val dbName = "my_app_db2"
| println(conf)
| }
defined class MyApp2
scala> new MyApp
Map(db -> my_app_db, foo -> bar)
res0: MyApp = MyApp@ccc268e
scala> new MyApp2
Map(db -> my_app_db2, foo -> bar, log -> false)
res1: MyApp2 = MyApp2@59d91aca
scala> new ConfApp with LogDisabled with LogEnabled {
| val dbName = "test1"
| println(conf)
| }
Map(db -> test1, foo -> bar, log -> true)
res2: ConfApp with LogDisabled with LogEnabled = $anon$1@16dfdeda
scala> new ConfApp with LogEnabled with LogDisabled {
| val dbName = "test2"
| println(conf)
| }
Map(db -> test2, foo -> bar, log -> false)
res3: ConfApp with LogEnabled with LogDisabled = $anon$1@420c2f4a
如果你需要一个 val conf
而不是 def conf
你可以这样做:
scala> class MyApp extends ConfApp {
| val dbName = "my_app_db"
| override val conf = super.conf
| println(conf)
| }
defined class MyApp
scala> new MyApp
Map(db -> my_app_db, foo -> bar)
res4: MyApp = MyApp@17ebbd2a
关于Scala - 特征成员初始化 : use traits to modify class member,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35359022/