我们可以在Java程序中编写Scala代码吗? [英] Can we write Scala code inside a Java program?

查看:161
本文介绍了我们可以在Java程序中编写Scala代码吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于基本问题,我已经有了一个大型的Java软件(我正在使用Eclipse Mars进行编辑),我想改变其中的一些类,并使用Scala而不是Java,这是可能吗?

解决方案

scala-lang.org首页


无缝JAVA INTEROP - Scala运行在JVM上,所以Java和


如果您点击它,它将为您提供进一步的信息:


将Scala和Java无缝结合



Scala类最终JVM类。您可以创建Java对象,调用其方法并从Scala透明地从Java类继承。类似地,Java代码可以引用Scala类和对象。



在这个例子中,Scala类Author实现了Java接口Comparable,并与Java文件一起使用。 Java代码使用来自协同对象Author的方法,并访问Author类的字段。它还使用JavaConversions在Scala集合和Java集合之间进行转换。


使用此示例:



Author.scala

  class Author(val firstName:String,
val lastName:String)extends Comparable [作者] {
override def compareTo(that:Author)= {
val lastNameComp = this.lastName compareTo that.lastName
if(lastNameComp!= 0)lastNameComp
else this.firstName compareTo that.firstName
}
}
对象作者{
def loadAuthorsFromFile(file:java.io .ile):列表[作者] = ???
}

App.java

  import static scala.collection.JavaConversions.asJavaCollection; 
public class App {
public List< Author> loadAuthorsFromFile(File file){
return new ArrayList< Author>(asJavaCollection(
Author.loadAuthorsFromFile(file)));
}
public void sortAuthors(List< Author> authors){
Collections.sort(authors);
}
public void displaySortedAuthors(File file){
列表<作者> authors = loadAuthorsFromFile(file);
sortAuthors(authors);
for(作者Author:authors){
System.out.println(
author.lastName()+,+ author.firstName());
}
}
}

你的例子可能是其他在Scala应用程序中使用Java类,但是由于在一天结束时它只是JVM类(上面的示例使用Java类,如 java.io.File ,在Scala代码中),这是一回事。


Sorry for the basic question, I have already a large software written in Java (I am using Eclipse Mars for editing) and I would like to change some classes in it and use Scala instead of Java, is such a thing possible?

解决方案

Right there on the scala-lang.org front page:

SEAMLESS JAVA INTEROP - Scala runs on the JVM, so Java and Scala stacks can be freely mixed for totally seamless integration.

And if you click it, it gives you this further information:

Combine Scala and Java seamlessly

Scala classes are ultimately JVM classes. You can create Java objects, call their methods and inherit from Java classes transparently from Scala. Similarly, Java code can reference Scala classes and objects.

In this example, the Scala class Author implements the Java interface Comparable and works with Java Files. The Java code uses a method from the companion object Author, and accesses fields of the Author class. It also uses JavaConversions to convert between Scala collections and Java collections.

With this example:

Author.scala:

class Author(val firstName: String,
    val lastName: String) extends Comparable[Author] {
  override def compareTo(that: Author) = {
    val lastNameComp = this.lastName compareTo that.lastName
    if (lastNameComp != 0) lastNameComp
    else this.firstName compareTo that.firstName
  }
}
object Author {
  def loadAuthorsFromFile(file: java.io.File): List[Author] = ???
}

App.java:

import static scala.collection.JavaConversions.asJavaCollection;
public class App {
    public List<Author> loadAuthorsFromFile(File file) {
        return new ArrayList<Author>(asJavaCollection(
            Author.loadAuthorsFromFile(file)));
    }
    public void sortAuthors(List<Author> authors) {
        Collections.sort(authors);
    }
    public void displaySortedAuthors(File file) {
        List<Author> authors = loadAuthorsFromFile(file);
        sortAuthors(authors);
        for (Author author : authors) {
            System.out.println(
                author.lastName() + ", " + author.firstName());
        }
    }
}

Your example is probably the other way around, using Java classes in a Scala app, but since at the end of the day it's just JVM classes (and the example above uses Java classes, like java.io.File, in the Scala code), it's the same thing.

这篇关于我们可以在Java程序中编写Scala代码吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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