【发布时间】:2019-12-28 17:26:18
【问题描述】:
我正在尝试声明一个基于某些函数的类型的类。
class A{
def getC = 10
}
class B {
def getC = 100
def getB = 9
}
def readC[T <: {def getC():Int}](obj:T) = {
obj.getC()
}
val a = new A()
val b = new B()
readC(a)
readC(b)
我希望 readC 应该与 A 和 B 一起使用。我也无法对 A 和 B 类进行更改,因此这里没有基于 trait 的解决方案。
还有,有没有更好的方法呢?
【问题讨论】:
-
Trait可以与adapter pattern结合使用
标签: scala typeclass type-constraints duck-typing structural-typing