Scala 2.8 中的 classOf[] 与 2.7 不同吗?
我有一个来自 Java 的接口,
public class IJava
{
...
public java.lang.Class getType();
...
}
它是在 Scala 中继承的,
class CScala
{
def getType() = classOf[Foo]
}
它在 Scala 2.7.7 中工作。但在 2.8.0.RC1 中,我得到了
type mismatch; found : java.lang.Class[Foo](classOf[Foo])
required: java.lang.Class
How do I get java.lang.Class
in Scala 2.8?
I have an interface from Java
public class IJava
{
...
public java.lang.Class getType();
...
}
It is inherited in Scala
class CScala
{
def getType() = classOf[Foo]
}
It worked in Scala 2.7.7. But in 2.8.0.RC1, I get
type mismatch; found : java.lang.Class[Foo](classOf[Foo])
required: java.lang.Class
How do I get java.lang.Class
in Scala 2.8?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试将
getType()
的返回类型注释为java.lang.Class
。这里的问题是你在接口上使用原始类型,而原始类型确实不能很好地与 Scala 配合使用。Try to annotate the return type of
getType()
asjava.lang.Class
. The problem here is that you are using a raw type on the interface, and raw types really don't work well with Scala.注释返回类型 java.lang.Class[Foo]
class CScala
{
def getType() : java.lang.Class[Foo] = classOf[Foo]
}
没关系。
但是,方法签名会被子类更改。有趣的!
annotate the return type java.lang.Class[Foo]
class CScala
{
def getType() : java.lang.Class[Foo] = classOf[Foo]
}
it is ok.
But, the method signature is changed by subclass. Interesting!