Overriding subclass methods with subclassed arguments?

流过昼夜 提交于 2019-11-30 04:46:42

问题


How can I force base methods to take in the same specific subclass instance when overriden by a subclass?

i.e.:

abstract class Animal {
  def mateWith(that: Animal)
}

class Cow extends Animal {
  override def mateWith...?
}

Logically, a Cow should only be able to mateWith another Cow. However, if I do override def mateWith(that: Cow), this doesn't actually override the base class method (which I want it to, since I want to enforce its existence in the subclass).

I could check to make sure the other instance is of type Cow, and throw an exception if it isn't - is this my best option? What if I have more animals? I would have to repeat the exception-throwing code.


回答1:


abstract class Animal[T <: Animal[T]] {
  def mateWith(that: T)
}

class Cow extends Animal[Cow] {
  override def mateWith(that: Cow) { println("cow") }
}

class Dog extends Animal[Dog] {
  override def mateWith(that: Dog) { println("dog") }
}

And use it like this:

scala> (new Cow).mateWith(new Cow)
cow

scala> (new Cow).mateWith(new Dog)
<console>:17: error: type mismatch;
 found   : Dog
 required: Cow
              (new Cow).mateWith(new Dog)
                                 ^

No exception-throwing code needed; the type system handles it for you at compile-time!



来源:https://stackoverflow.com/questions/10199254/overriding-subclass-methods-with-subclassed-arguments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!