Scala等效于Java java.lang.Class< T>目的 [英] Scala equivalent of Java java.lang.Class<T> Object

查看:169
本文介绍了Scala等效于Java java.lang.Class< T>目的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题最好用一个例子来解释:

The question is best explained by an example:

在Java中,对于JPA EntityManager,我可以做下面的事情(Account是我的Entity类):

In Java for a JPA EntityManager, I can do the following(Account is my Entity class):

Account result = manager.find(Account.class, primaryKey);



在Scala中,我的天真尝试是:

In Scala, my naive attempt is:

val result = manager.find(Account.class, primaryKey)

但是当我尝试在Scala中使用 Account.class 时,似乎不喜欢这样。如何在Scala中为Account类指定java.lang.Class对象?

But when I try to use Account.class in Scala, it seems to not like this. How can I specify the java.lang.Class object for the Account class in Scala?

推荐答案

根据 Scala类型系统

val c = new C
val clazz = c.getClass              // method from java.lang.Object
val clazz2 = classOf[C]             // Scala method: classOf[C] ~ C.class
val methods = clazz.getMethods      // method from java.lang.Class<T>




classOf [T] 方法返回Scala类型的运行时表示。它类似于Java表达式 T.class

使用 classOf [T] 方便,当你有一个类型,你想要的信息,而 getClass 方便从类型的实例检索相同的信息。

The classOf[T] method returns the runtime representation for a Scala type. It is analogous to the Java expression T.class.
Using classOf[T] is convenient when you have a type that you want information about, while getClass is convenient for retrieving the same information from an instance of the type.

但是, classOf [T] getClass 不同的值,反映类型擦除对JVM的影响,在getClass的情况下。

However, classOf[T] and getClass return slightly different values, reflecting the effect of type erasure on the JVM, in the case of getClass.

scala> classOf[C]
res0: java.lang.Class[C] = class C

scala> c.getClass
res1: java.lang.Class[_] = class C

是为什么无法正常工作

val xClass: Class[X] = new X().getClass //it returns Class[_], nor Class[X]

val integerClass: Class[Integer] = new Integer(5).getClass //similar error

有关 getClass 的返回类型的机票code>

James Moore 报告票是现在,即2011年11月,两年后,固定。

在2.9.1中, getClass 现在有:

(James Moore reports that the ticket is "now", ie Nov. 2011, two years later, fixed.
In 2.9.1, getClass now does:

scala> "foo".getClass 
       res0: java.lang.Class[_ <: java.lang.String] = class java.lang.String

回到2009年:


如果Scala将getClass一个java.lang.Class [T] forSome {val T:C}其中C类似于调用getClass的表达式的静态类型的擦除

It would be useful if Scala were to treat the return from getClass() as a java.lang.Class[T] forSome { val T : C } where C is something like the erasure of the static type of the expression on which getClass is called

它会让我做一些类似下面的事情,我想内省一个类,但不应该需要一个类实例。

我也想限制类的类型,我想introspect,所以我使用类[_< ;: Foo]。但这阻止我通过使用Foo.getClass()没有传递一个Foo类。

It would let me do something like the following where I want to introspect on a class but shouldn't need a class instance.
I also want to limit the types of classes I want to introspect on, so I use Class[_ <: Foo]. But this prevents me from passing in a Foo class by using Foo.getClass() without a cast.

注意:关于 getClass ,可能的解决方法是:

Note: regarding getClass, a possible workaround would be:

class NiceObject[T <: AnyRef](x : T) {
  def niceClass : Class[_ <: T] = x.getClass.asInstanceOf[Class[T]]
}

implicit def toNiceObject[T <: AnyRef](x : T) = new NiceObject(x)

scala> "Hello world".niceClass                                       
res11: java.lang.Class[_ <: java.lang.String] = class java.lang.String

这篇关于Scala等效于Java java.lang.Class&lt; T&gt;目的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆