Scala相当于Java java.lang.Class 对象(Scala equivalent of Java java.lang.Class Object)

编程入门 行业动态 更新时间:2024-10-16 18:37:20
Scala相当于Java java.lang.Class 对象(Scala equivalent of Java java.lang.Class Object)

这个问题最好由一个例子解释:

在JPA EntityManager的Java中,我可以执行以下操作(Account是我的Entity类):

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

在斯卡拉,我天真的尝试是:

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

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

The question is best explained by an example:

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

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

In Scala, my naive attempt is:

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

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便于从类型的实例中检索相同的信息。

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

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的返回类型有一张票 。

( 詹姆斯·摩尔报道说,票是“现在”,即2011年11月,两年后,固定。 在2.9.1中, getClass现在可以:

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表达式的静态类型的擦除叫

它会让我做一些类似于以下的事情,我想在类中进行内省,但不应该需要一个类实例。 我也想限制我想要内省的类的类型,所以我使用Class [_ <:Foo]。 但是这样可以防止我在没有转换的情况下使用Foo.getClass()传入Foo类。

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

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

According to "The Scala Type System",

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>

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.

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

That is why the following will not work:

val xClass: Class[X] = new X().getClass //it returns Class[_], nor Class[X] val integerClass: Class[Integer] = new Integer(5).getClass //similar error

There is a ticket regarding the return type of 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

)

Back in 2009:

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

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.

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

更多推荐

本文发布于:2023-07-14 14:29:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1105102.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   java   Scala   Java   lang

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!