有没有办法在 scala 中 "program to interface"
Is there a way to "program to interface" in scala
有没有办法在 Scala 中 "program to interface"?
我是 Scala 新手。我在 scala 中有一个 "trait" 并且许多 classes 扩展了该特征。我正在寻找一种关于如何使用界面而不是直接使用 class 的方法。
目前,我在做:
val clazz = new MyClass()
其中 MyClass 从特征扩展而来。
我正在寻找一种方法 return 接口作为工厂方法的一个实例来生成 classes。有办法吗?
您始终可以只请求接口类型:
val clazz: MyInterf = new MyClass()
如果你真的想隐藏实现,一种方法是为特征创建一个伴侣:
trait MyInterf {
...
}
class MyClass extends MyInterf {
...
}
object MyInterf {
def create: MyIntef = new MyClass()
}
// calling code
val impl = MyInterf.create
有没有办法在 Scala 中 "program to interface"?
我是 Scala 新手。我在 scala 中有一个 "trait" 并且许多 classes 扩展了该特征。我正在寻找一种关于如何使用界面而不是直接使用 class 的方法。
目前,我在做:
val clazz = new MyClass()
其中 MyClass 从特征扩展而来。
我正在寻找一种方法 return 接口作为工厂方法的一个实例来生成 classes。有办法吗?
您始终可以只请求接口类型:
val clazz: MyInterf = new MyClass()
如果你真的想隐藏实现,一种方法是为特征创建一个伴侣:
trait MyInterf {
...
}
class MyClass extends MyInterf {
...
}
object MyInterf {
def create: MyIntef = new MyClass()
}
// calling code
val impl = MyInterf.create