java - 从 Java 访问时,Scala 公共变为私有?
问题描述
我对Java和Maven非常熟悉,但对Scala 一点也不熟悉。我正在尝试从Java访问Scala ,我似乎无法理解的问题之一如下:
我的问题
在AccessScala.java
我尝试访问一个变量WithinModule.scala
:
package me.parent.jModule;
public class AccessScala{
public static void main(String[] args){
WithinModule within = new WithinModule();
// -- this is like a static variable
System.out.println(WithinModule.string);
// -- this is like an instance variable
System.out.println(within.string);
// -- this is a getter, as it should be in Java
System.out.println(within.getString());
}
}
这个变量应该是公开的
package me.parent.jModule;
class WithinModule{
val string : String = "this is the local scala string";
}
但是在运行时mvn clean package
它告诉我这个变量是私有的:
[ERROR] [Error] C:\projectLocation\parent\jModule\src\main\java\me\parent\jModule\AccessScala.java:7: string has private access in me.parent.jModule.WithinModule
[ERROR] [Error] C:\projectLocation\parent\jModule\src\main\java\me\parent\jModule\AccessScala.java:9: string has private access in me.parent.jModule.WithinModule
基本上,我是误解了这应该如何工作,还是我做错了什么?
项目设置
我将项目放在GitHub 上,并在此处总结了可能相关的内容。文件是这样组织的
> parent
| > jModule
| | > src/main
| | | > java/me/parent/jModule
| | | | > AccessScala.java
| | | > scala/me/parent/jModule
| | | | > WithinModule.scala
| | > pom.xml
| > sModule
| | > src/scala/me/parent/sModule
| | | >ExternalModule.scala
| | > pom.xml
| > pom.xml
pom.xml
我主要从这里带走了我的父母。经过一些修改,它最终如下所示。这种确切的设置在另一个项目上运行良好,其中(到目前为止)只有一个案例Java使用了用Scala编写的类,但两种语言都有大量代码。测试一切正常,直到我们尝试在Java中从Scalaval
访问s 。
我使用 Maven编译器、surefire和failsafe插件来编译和测试 Java(还没有测试)和Scala-Maven和ScalaTest插件来编译和测试 Scala 代码(也没有测试)。它们被配置为首先编译和测试 Scala,然后转向 Java。
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>me</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.5.1</version>
</dependency>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>2.13.1</version>
</dependency>
<dependency>
<groupId>org.scalatest</groupId>
<artifactId>scalatest_2.13</artifactId>
<version>3.1.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- stuff for compiling and testing Java during `mvn package` -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>3.0.0-M3</version>
</plugin>
<!-- This is for Scala compilation during `mvn package` -->
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>4.3.1</version>
<executions>
<execution>
<id>scala-compile-first</id>
<phase>process-resources</phase>
<goals>
<goal>add-source</goal>
<goal>compile</goal>
</goals>
</execution>
<!--
<execution>
<id>scala-test-compile</id>
<phase>process-test-resources</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
-->
</executions>
</plugin>
<plugin>
<groupId>org.scalatest</groupId>
<artifactId>scalatest-maven-plugin</artifactId>
<version>1.0</version>
<configuration>
<reportsDirectory>${project.build.directory}/surefire-reports</reportsDirectory>
<junitxml>.</junitxml>
<filereports>WDF TestSuite.txt</filereports>
</configuration>
<executions>
<execution>
<id>test</id>
<phase>process-test-resources</phase>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<modules>
<module>jModule</module>
<module>sModule</module>
</modules>
</project>
附言
任何建议将不胜感激。正如我所提到的,我试图让 Java 和 Scala 一起工作,我听说这应该很容易,但我一直遇到问题,这就是其中之一。
解决方案
分析scalac -print
输出我们看到
class WithinModule {
val string : String = "this is the local scala string"
}
扩展到类似的东西
class iw$WithinModule extends Object {
private[this] val string: String = _;
<stable> <accessor> def string(): String = iw$WithinModule.this.string;
def <init>(): $line184.iw$WithinModule = {
iw$WithinModule.super.<init>();
iw$WithinModule.this.string = "this is the local scala string";
()
}
}
我们注意到的地方def string(): String
,因此在AccessScala.java
尝试
WithinModule within = new WithinModule();
System.out.println(within.string());
推荐阅读
- vue-cli-4 - Vue 给我一个关于未提交更改的警告
- swiftui - 停止 swiftUI 动画并跳转到目标值
- json - 如何在 Spring Boot 应用程序中使用 Apache Camel 使用 json 模式验证消息体
- azure - 如何获取用于 CI/CD 部署的 Azure 密钥保管库机密 URI?
- python - 在搜索引擎中使用 ScikitLearn TfidfVectorizer
- java - 基于 Spring Security XML 的配置:java.lang.IllegalArgumentException:没有为 id“null”映射 PasswordEncoder
- r - 如何通过使用'fileInput'函数来绘制ggplot来提供上传的数据?
- php - 我如何将我的日期导入数据库 incule \ 和,?
- ios - 按下行时未调用表视图委托方法 (didSelectRowAt)
- bash - 获取目录中唯一文件的完整路径