我已经学习Scala用于网络开发很长一段时间了,我偶然发现缺少接口.来自PHP,我使用接口相当多的方法级多态和IoC像这样:
interface iAnimal
{
    function makeVoice();
}
class Cat implements iAnimal
{
    function makeVoice()
    {
        return "Meow";
    }
}
class Dog implements iAnimal
{
    function makeVoice()
    {
        return "Woof!";
    }
}
class Box
{
    private $_animal;
    function __construct(iAnimal $animal)
    {
        $this->_animal = $animal;         
    }
    function makeSound()
    {
        echo $this->_animal->makeVoice();
    }
}
等等,这是一种简单的方法来确保我传递给Box对象的任何东西都有一个makeVoice我在其他地方调用的方法.现在,我很好奇的是,我如何使用Scala实现类似的功能.我试着寻找这个,但信息非常稀少.我找到的唯一答案是使用特征,但据我所知,它们用于具体实现,而不是声明.
提前致谢.
根据其他答案,解决方案是使用特征:
trait Animal {
  def makeVoice(): Unit //no definition, this is abstract!
}
class Cat extends Animal{
  def makeVoice(): Unit = "Meow"
}
class Dog extends Animal{
  def makeVoice(): Unit = "Woof"
}
class Box(animal:Animal) {
  def makeSound() = animal.makeVoice()
}
traitScala中的A 将直接编译为interfaceJava.如果它包含任何具体成员,那么这些将直接复制到任何继承该特征的类中.您可以愉快地使用Scala特性作为Java的接口,但是您没有为您混合使用具体功能.
但是......这只是图片的一部分.到目前为止我们实现的是亚型多态性,Scala还允许ad-hoc多态(也称为类型类):
// Note: no common supertype needed here
class Cat { ... }
class Dog { ... }
sealed trait MakesVoice[T] {
  def makeVoice(): Unit
}
object MakesVoice {
  implicit object CatMakesVoice extends MakesVoice[Cat] {
    def makeVoice(): Unit = "Meow"
  }
  implicit object DogMakesVoice extends MakesVoice[Dog] {
    def makeVoice(): Unit = "Woof"
  }
  //helper method, not required, but nice to have
  def makesVoice[T](implicit mv: MakesVoice[T]) = mv
}
import MakesVoice._
//context-bound version
class Box[T : MakesVoice] {
  //using a helper:
  def makeSound() = makesVoice[T].makeVoice()
  //direct:
  def makeSound() = implicitly(MakesVoice[T]).makeVoice()
}
//using an implicit param
class Box[T](implicit mv : MakesVoice[T]) {
  def makeSound() = mv.makeVoice()
}
这里重要的是MakesVoice类型类可以与任何类型相关联,无论它属于哪个层次结构.您甚至可以使用从第三方库导入的基元或类型的类型类,您无法使用新接口进行改造.
当然,你也有参数多态性,你可能更熟悉"泛型":)