从 Java 调用带有参数和内部类的 scala 抽象类

发布于 2024-11-13 11:29:06 字数 977 浏览 2 评论 0原文

如果我定义一个 Scala 类:

 class X(i:Int) {
   println (i)
 }

如何在 Java 代码中使用该类?

[编辑]实际上,我的问题稍微复杂一些,

我有一个抽象类,

 abstract class X(i:Int) {
   println (i)
   def hello(s:String):Unit
 }

我需要在Java代码中使用它。可以轻松做到吗?

[EDIT2] 考虑以下代码

 object B {
    case class C(i:Int)
 }
 abstract class X(i:Int) {
   println (i)
   def hello(a:B.C):Unit
 }

在这种情况下,以下 java 代码在 Netbeans IDE 中给出错误,但构建正常:

 public class Y extends X  {
    public void hello(B.C c) {
       System.out.println("here");
    }
    public Y(int i) {
       super(i);
    }
 }

我得到的错误是:

hello(B.C) in Y cannot override hello(B.C) in X; overridden method is static, final

Netbeans 6.8,Scala 2.8。

到目前为止,我认为唯一的解决方案是忽略 NB 错误。

这是一张显示我得到的确切错误的图像: 使用 java 中的 scala 代码时出现 IDE 错误

If I define a Scala class:

 class X(i:Int) {
   println (i)
 }

How do I use this class in Java code?

[EDIT] Actually, my problem is slightly more complicated

I have an abstract class

 abstract class X(i:Int) {
   println (i)
   def hello(s:String):Unit
 }

I need to use this in Java code. Is it possible to do it easily?

[EDIT2] Consider the following code

 object B {
    case class C(i:Int)
 }
 abstract class X(i:Int) {
   println (i)
   def hello(a:B.C):Unit
 }

In this case, the following java code gives an error in Netbeans IDE but builds fine:

 public class Y extends X  {
    public void hello(B.C c) {
       System.out.println("here");
    }
    public Y(int i) {
       super(i);
    }
 }

The error I get is:

hello(B.C) in Y cannot override hello(B.C) in X; overridden method is static, final

Netbeans 6.8, Scala 2.8.

As of now I think the only solution is to ignore the NB errors.

Here is an image showing the exact error(s) I get:
IDE error using scala code from java

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

倚栏听风 2024-11-20 11:29:06

为您的类生成的字节码将与 Java 定义相同

abstract class X implements scala.ScalaObject {
  public X(int i) {
    System.out.println(i);
  }

  public abstract void hello(String s);

  //possibly other fields/methods mixed-in from ScalaObject
}

完全按照使用等效 Java 的方式使用它;子类并提供 hello 方法的具体实现。

The generated bytecode for your class will be identical to the Java definition:

abstract class X implements scala.ScalaObject {
  public X(int i) {
    System.out.println(i);
  }

  public abstract void hello(String s);

  //possibly other fields/methods mixed-in from ScalaObject
}

Use it exactly as you would for this equivalent Java; subclass and provide a concrete implementation of the hello method.

江湖正好 2024-11-20 11:29:06

您需要在类路径中传递 Scala 库 jar 来编译扩展 Scala 类的任何 Java 代码。例如:

dcs@ayanami:~/tmp$ cat X.scala
abstract class X(i:Int) {
   println (i)
   def hello(s:String):Unit
}

dcs@ayanami:~/tmp$ scalac X.scala
dcs@ayanami:~/tmp$ cat Y.java
public class Y extends X {
    public Y(int i) {
        super(i);
    }

    public void hello(String s) {
        System.out.println("Hello "+s);
    }
}
dcs@ayanami:~/tmp$ javac -cp .:/home/dcs/github/scala/dists/latest/lib/scala-library.jar Y.java

You need to pass the Scala library jar in the class path to compile any Java code extending a Scala class. For example:

dcs@ayanami:~/tmp$ cat X.scala
abstract class X(i:Int) {
   println (i)
   def hello(s:String):Unit
}

dcs@ayanami:~/tmp$ scalac X.scala
dcs@ayanami:~/tmp$ cat Y.java
public class Y extends X {
    public Y(int i) {
        super(i);
    }

    public void hello(String s) {
        System.out.println("Hello "+s);
    }
}
dcs@ayanami:~/tmp$ javac -cp .:/home/dcs/github/scala/dists/latest/lib/scala-library.jar Y.java
秋千易 2024-11-20 11:29:06

您应该能够从 Java 调用 Scala 代码(可能需要进行一些名称修改等)。如果您尝试以下操作会发生什么?

X x = new X(23);

在更复杂的情况下,您可以使用 javap 或类似工具来了解 Scala 代码是如何翻译的。如果没有其他办法,您仍然可以在 Scala 端编写一些辅助方法,以便更轻松地从 Java 端进行访问(例如,当涉及隐式时)。

You should be able to call Scala code from Java (maybe with some name mangling and so on). What happens if you try the following?

X x = new X(23);

In more complicated cases you can use javap or similar tools to find out how the Scala code got translated. If nothing else works, you can still write some helper methods on Scala side to make access from Java side easier (e.g. when implicits are involved).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文