在 Play 框架中混合使用 scala 和 java [英] Mixing scala and java in Play Framework

查看:25
本文介绍了在 Play 框架中混合使用 scala 和 java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的 Java 文件:

I have a Java file that looks like this:

package AuthorizeNetFingerprint;


class Fingerprint {
    private static Log logger = LogFactory.getLog(Fingerprint.class);

    private long sequence;
    private long timeStamp;
    private String fingerprintHash;

    private Fingerprint() {
    }

    /**
     * Creates a fingerprint with raw data fields.
     * 
     * @param loginID
     * @param transactionKey
     * @param sequence : this number will be concatenated with a random value
     * @param amount
     * @return A Fingerprint object.
     */
    public static String createFingerprint(String loginID,
                    String transactionKey, long sequence, String amount) {
         return transactionKey;
    }
}

我正试图像这样访问它:

And I am trying to access it like this scala:

val fingerprint = new AuthorizeNetFingerprint.Fingerprint
val x_fp_hash = fingerprint.createFingerprint(x_api_login_id,
                  transaction_key, x_fp_sequence, x_amount)

它给了我这个错误:

无法在包 AuthorizeNetFingerprint 中访问包 AuthorizeNetFingerprint 中的对象指纹

object Fingerprint in package AuthorizeNetFingerprint cannot be accessed in package AuthorizeNetFingerprint

是否可以在 Play Framework 中混合使用 Scala 和 Java?

Is it possible to mix scala and java in Play Framework?

我做错了什么?

编辑

我需要:公开课指纹

代替

class Fingerprint

推荐答案

三件事:

  1. 您已经知道,您的指纹类需要公开.
  2. 您已将 Fingerprint 的构造函数设为私有;你不能实例化它.
  3. Java 类中的任何静态方法都应通过该类在 Scala 中的伴随对象进行访问.

示例中的所有 Scala 代码都应替换为:

All the Scala code in your example should be replaced by:

val x_fp_hash = AuthorizeNetFingerprint.Fingerprint.createFingerprint(…)

这适用于使用 sbt (0.11.3) 编译的 Scala (2.9.1) 控制台.

This works in the Scala (2.9.1) console, compiled with sbt (0.11.3).

是的,您可以在 Play2 应用程序中混合使用 Java 和 Scala,只需将 Java 代码放在 app 目录中即可.请注意,Java 类需要在其对应的包目录中,而 Scala 类则不然.

Yes, you can mix Java and Scala in a Play2 application, just put the Java code in the app directory. Note that Java classes need to be in their corresponding package directories, which is not the case for Scala classes.

这篇关于在 Play 框架中混合使用 scala 和 java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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