我只是想知道是否可以在 Scala 中迭代一个密封的特征?如果不是,为什么不可能?既然特征是密封的,应该是可能的吧?
我想做的是这样的:
sealed trait ResizedImageKey {
/**
* Get the dimensions to use on the resized image associated with this key
*/
def getDimension(originalDimension: Dimension): Dimension
}
case class Dimension(width: Int, height: Int)
case object Large extends ResizedImageKey {
def getDimension(originalDimension: Dimension) = Dimension(1000,1000)
}
case object Medium extends ResizedImageKey{
def getDimension(originalDimension: Dimension) = Dimension(500,500)
}
case object Small extends ResizedImageKey{
def getDimension(originalDimension: Dimension) = Dimension(100,100)
}
我想要的可以通过给枚举值提供一个实现在 Java 中完成。Scala中是否有等价物?