Last active
December 14, 2015 23:49
-
-
Save yangbajing/5168243 to your computer and use it in GitHub Desktop.
Scala 也有像C++一样的“方法隐藏”这种情况?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 代码: | |
| trait A { | |
| def find: String = find("nnn") | |
| def find(s: String): String | |
| } | |
| object B extends A { | |
| def find(s: String) = s + " - B" | |
| } | |
| object C extends App { | |
| val y = B.find("yangbajing") | |
| val x = B.find | |
| } | |
| // 编译错误: | |
| t.scala:15: error: ambiguous reference to overloaded definition, | |
| both method find in object B of type (s: String)String | |
| and method find in trait A of type => String | |
| match expected type ? | |
| val x = B.find | |
| ^ | |
| one error found | |
| //临时解决方案(求更好的方法): | |
| 在B对象中重定义: | |
| override def find = super.find |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment