我在Java中有以下课程:
public class GeneralClass {
public Integer getInt(Double d) {
return d.intValue();
}
}
我想在scala中扩展此类,例如
import scala.collection.JavaConversions._
import scala.collection.JavaConverters._
class SpecializedClass extends GeneralClass {
override def getInt(d: Double): Int = {
d.intValue()
}
}
问题是scala.Double和scala.Int都不会自动转换为各自的Java类型.
编辑
错误消息是:
method getInt overrides nothing. Note: the super classes of class
SpecializedClass contain the following, non final members named
getInt: def getInt(d: Double): Integer
有人可以解释一下,也许可以指出一个优雅的解决方案?
解决方法:
SpecializedClass中的getInt不会覆盖GeneralClass中的方法.为了能够覆盖,SpecializedClass中的函数参数应该是协变的,而返回类型应该是协变的.您的情况不是.
因此,除非您在SpecializedClass的getInt中包含类型T的内容: java.lang.Double并返回类型为T<:java.lang.Integer,它将不会覆盖它. 对于您的问题,实际上我不认为需要这样的方法,例如Scala编译器会将Java double隐式转换为Scala Double.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。