Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
假设我有一个A应该与 trait 混合的类,应该B是B或者B1基于B2一个标志b1:
A
B
B1
B2
b1
val b1: Boolean type B = if (b1) B1 else B2 // impossible Scala code class A extends B
有没有办法根据条件“动态”混合特征?
类型是静态的东西,其定义在编译时固定。但是,您可以使用 if / else 逻辑创建变体匿名类的实例:
val a = if (b) new A with B1 else new A with B2