Scala 2.8 中的 classOf[] 与 2.7 不同吗?

发布于 2024-08-29 09:41:38 字数 449 浏览 4 评论 0原文

我有一个来自 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

守护在此方 2024-09-05 09:41:38

尝试将getType()的返回类型注释为java.lang.Class。这里的问题是你在接口上使用原始类型,而原始类型确实不能很好地与 Scala 配合使用。

Try to annotate the return type of getType() as java.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.

江城子 2024-09-05 09:41:38

注释返回类型 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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文