scala – 在特征中实现方法的规则是什么?

我定义了一个特征:

trait A {
   def hello(name:Any):Any
}

然后定义一个类X来实现它:

class X extends A {
  def hello(name:Any): Any = {}
}

它汇编了.然后我更改子类中的返回类型:

class X extends A {
  def hello(name:Any): String = "hello"
}

它也汇编了.然后更改参数类型:

class X extends A {
  def hello(name:String): Any = {}
}

这次无法编译,错误是:

error: class X needs to be abstract,since method hello in trait A of type (name: Any)
Any is not defined
(Note that Any does not match String: class String in package lang is a subclass 
of class Any in package scala,but method parameter types must match exactly.)

看起来参数应该完全匹配,但返回类型可以是子类中的子类型?

更新:@ Mik378,感谢您的回答,但为什么以下示例无效?我认为它不会打破Liskov:

trait A {
   def hello(name:String):Any
}

class X extends A {
   def hello(name:Any): Any = {}
}

解决方法

就像在Java中一样,为了保持 Liskov Substitution principle,你不能用更精细的参数覆盖一个方法.

实际上,如果您的代码处理A类型,引用X类型的内容,该怎么办?
根据A,你可以传递任何你想要的类型,但B只允许String.
因此=>繁荣

逻辑上,使用相同的推理,允许更细粒度的返回类型,因为任何处理A类的代码都可以覆盖任何情况.

您可能想要检查这些部分:
http://en.wikipedia.org/wiki/Covariance_and_contravariance_(computer_science)#Covariant_method_return_type

http://en.wikipedia.org/wiki/Covariance_and_contravariance_(computer_science)#Contravariant_method_argument_type

UPDATE —————-

trait A {
   def hello(name:String):Any
}

class X extends A {
   def hello(name:Any): Any = {}
}

它将充当一个完美的超载,而不是压倒一切.

相关文章

共收录Twitter的14款开源软件,第1页Twitter的Emoji表情 Tw...
Java和Scala中关于==的区别Java:==比较两个变量本身的值,即...
本篇内容主要讲解“Scala怎么使用”,感兴趣的朋友不妨来看看...
这篇文章主要介绍“Scala是一种什么语言”,在日常操作中,相...
这篇文章主要介绍“Scala Trait怎么使用”,在日常操作中,相...
这篇文章主要介绍“Scala类型检查与模式匹配怎么使用”,在日...