简体   繁体   English

我可以使用scala类来实现Java的Java接口吗?

[英]Can I use a scala class which implements a java interface from Java?

I'm learning Scala and was curious if it would be possible to: 我正在学习Scala并且很好奇是否有可能:

  1. Create an object which implements a Java interface in Scala 创建一个在Scala中实现Java接口的对象
  2. Compile the object into a class file and jar it 将对象编译为类文件并将其jar
  3. Use the object from Java 使用Java中的对象

I want to implement a custom lucene query parser in scala and be able to let others access it from a java application. 我想在scala中实现自定义lucene查询解析器,并允许其他人从java应用程序访问它。

I'm assuming that by "object" you actually mean "class". 我假设通过“对象”你实际上意味着“阶级”。 In any case, the answer is yes, you can do this. 无论如何,答案是肯定的,你可以这样做。 You will need to take advantage of the Scala/Java joint compiler if you want this all in the same project. 如果您希望在同一个项目中使用Scala / Java联合编译器,则需要利用它。 For example: 例如:

public interface Parser {
    public TokenIterator parse(InputStream is);
}

Then in Scala: 然后在斯卡拉:

class ParserImpl extends Parser {
  def parse(is: InputStream) = ...
}

Finally, in Java again: 最后,再次在Java中:

public class Consumer {
    public static void main(String[] args) {
        Parser p = new ParserImpl();       // just like a Java class!
        ...
    }
}

If all of these source files are in the same project, then you will want to compile them using the following invocation of commands: 如果所有这些源文件都在同一个项目中,那么您将需要使用以下命令调用来编译它们:

$ scalac *.scala *.java
$ javac -classpath . *.java

The first command invokes the Scala/Java joint compiler, which will compile the Scala sources and just enough of the Java sources to satisfy any dependencies. 第一个命令调用Scala / Java联合编译器,它将编译Scala源和足够的Java源以满足任何依赖性。 The second command invokes the Java compiler with the recently-compiled Scala classes on the classpath. 第二个命令使用类路径上最近编译的Scala类调用Java编译器。

I have implemented a custom org.apache.solr.handler.RequestHandlerBase and custom org.apache.solr.request.QueryResponseWriter in Scala 2.8 with Java 1.6.0 and Solr 1.4.0. 我在Java 1.6.0和Solr 1.4.0的Scala 2.8中实现了自定义org.apache.solr.handler.RequestHandlerBase和自定义org.apache.solr.request.QueryResponseWriter I am not sure of the Lucene version Solr 1.4.0 is using but I would assume a fairly recent one. 我不确定Solce 1.4.0正在使用的Lucene版本,但我会假设一个相当新的版本。 Anyway, I packaged the class files generated by Scala in a jar and Solr was able to load it just fine. 无论如何,我将Scala生成的类文件打包在一个jar中,Solr能够正确加载它。 So what you are trying to do is possible. 所以你想做的事情是可能的。

I would say the main thing to overcome is to convert from Java collection objects like List and Vector to Scala collection objects and vice versa. 我想说要解决的主要问题是将Java集合对象(如ListVector为Scala集合对象,反之亦然。 scala.collection.JavaConversions in Scala 2.8.0 will probably be your friend. Scala scala.collection.JavaConversions中的scala.collection.JavaConversions可能是你的朋友。

One other thing, if you need to override a method like getBooleanQuery , you may need to add [_] to make Scala compile (code not tested - but since it took me a while to figure that kind of stuff out when I started Scala, I thought I'd share): 另外一件事,如果你需要覆盖像getBooleanQuery这样的方法,你可能需要添加[_]来使Scala编译(代码没有经过测试 - 但是因为我开始使用Scala时花了一些时间来计算这种东西,我以为我会分享):

override def getBooleanQuery(clauses: java.util.List[_]): Query = { ... }

Edit: : One more thing about the joint compilation. 编辑:关于联合编译还有一件事。 Daniel's information on how to joint compile is useful. Daniel关于如何联合编译的信息很有用。 You may not immediately need it though, as very likely, you just need to have scalac know the path to the Lucene jar files, then package your class in a new jar, and have your jar file used at runtime in the deployed environment. 您可能不会立即需要它,因为很可能,您只需要让scalac知道Lucene jar文件的路径,然后将您的类打包到一个新jar中,并在部署环境中的运行时使用您的jar文件。

Yes it's possible. 是的,这是可能的。 Some of the Scala features (such as singleton objects) will compile to code which is hard to use from Java, but using Scala classes through a Java interface is an easy way to write compatible code. 一些Scala特性(例如单例对象)将编译为难以从Java使用的代码,但是通过Java接口使用Scala类是编写兼容代码的简单方法。

Some questions about Java interoperability are answered at http://www.scala-lang.org/faq/4 and in the book Programming in Scala . 有关Java互操作性的一些问题, 请参见http://www.scala-lang.org/faq/4和“ Scala编程 ”一书。 Quoted from that FAQ: 引用该FAQ:

Can I use existing Java code from Scala? 我可以使用Scala的现有Java代码吗?

Accessing Java classes from Scala code is no problem at all. 从Scala代码访问Java类完全没问题。 Using a Scala class from Java can get tricky, in particular if your Scala class uses advanced features like generics, polymorphic methods, or abstract types. 使用Java中的Scala类可能会变得棘手,特别是如果您的Scala类使用泛型,多态方法或抽象类型等高级功能。 Since Java doesn't have such language features, you have to know something about the encoding scheme of Scala classes. 由于Java没有这样的语言功能,因此您必须了解Scala类的编码方案。 Otherwise, there should be no problems. 否则,应该没有问题。

Do I need to convert Java data structures to Scala, and vice versa? 我是否需要将Java数据结构转换为Scala,反之亦然?

You do not have to convert Java data structures at all for using them in Scala. 您根本不必转换Java数据结构以在Scala中使用它们。 You can use them "as is". 您可以“按原样”使用它们。 For instance, Scala classes can subclass Java classes, you can instantiate Java classes in Scala, you can access methods, fields (even if they are static), etc. 例如,Scala类可以子类化Java类,可以在Scala中实例化Java类,可以访问方法,字段(即使它们是静态的)等。

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

相关问题 为什么我不能从Scala访问实现Java接口的类中声明的变量? - Why can't I access a variable declared in a class, which implements a Java interface, from Scala? 如何从实现我的接口的JAR归档中加载类? (java) - How can I load a class from a JAR-archive which implements my interface? (java) Java:从实现接口的类访问接口函数 - Java: Accessing a interface function from a class which implements the interface 实现接口的 Java class 可以自动继承注释吗? - Can a Java class which implements an interface inherit the annotations automatically? 如何实例化在Java中实现Java接口的JRuby类 - How can I instantiate a JRuby class that implements a Java interface in Java Java实例抽象类,实现接口 - Java instance abstract class, which implements interface 实现泛型接口的Java泛型类 - Java Generic Class which Implements a Generic Interface 在Jython中,我可以创建一个实现Java接口的内联匿名类吗? - In Jython, can I make an inline anonymous class that implements a Java interface? 从实现接口的类中调用方法(java) - calling a method from a class that implements an interface (java) 使用Java泛型来接受和接口并返回实现该接口的类 - Use Java generics to accept and Interface and return a Class that implements that Interface
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM