加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-16 09:53:46 所属栏目:安全 来源:网络整理
导读:我定义了一个特征: 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"} 它也汇编了.
我定义了一个特征:

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 = {}
}

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读