简体   繁体   English

如何在scala中指定Foo.class?

[英]How to specify Foo.class in scala?

I am using java API that looks like: 我使用的Java API看起来像:

import com.google.code.morphia.Morphia;

.....

     val morphia = new Morphia();
     morphia.map(Hotel.class).map(Address.class);

but it gives scala compiler error. 但它给scala编译器错误。 what is the correct code in scala for above? 上面scala中的正确代码是什么? Note that .map is defined as part of morphia API and not to be confused with scala map. 请注意,.map被定义为morphia API的一部分,不要与scala map混淆。

Assuming your problem is with the .class parts, the Scala equivalent of Hotel.class would be classOf[Hotel] , likewise for Address . 假设您的问题与.class部分有关,那么Hotel.class的Scala等价物将是classOf[Hotel] ,同样适用于Address

Hopefully this should fix your problem, although it's hard to tell without seeing the full compiler output. 希望这可以解决你的问题,虽然很难说没有看到完整的编译器输出。

你应该使用classOf[Hotel]

Edit : After having looked at the API; 编辑 :看了API之后; what you actually want is: 你真正想要的是:

classOf[T]

eg. 例如。

classOf[Hotel]

More info on Scala's typesystem. 有关 Scala类型系统的更多信息


Wrong answer, but maybe useful, if someone is mislead by this questions title: 错误的答案,但也许有用,如果有人误导这个问题标题:

class is a reserved word in Scala. class是Scala中的保留字。 If you want to use it as a field or method name, you need to enclose it with backticks: 如果要将其用作字段或方法名称,则需要使用反引号将其括起来:

Hotel.`class`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM