Java不允许我使用records。

huangapple go评论82阅读模式
英文:

Java is not letting me use records

问题

我正在运行 JDK 15刚刚下载了它),并且在尝试使用记录records时遇到编译错误

    public class Runner {
    
    	// 这会导致编译错误
        record Point(int x, int y) {}
    
    }

以下是我的 pom.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<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>something</groupId>
    <artifactId>other-thing</artifactId>
    <version>1.0.0-SNAPSHOT</version>

    <properties>
        <java.version>15</java.version>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

<details>
<summary>英文:</summary>

I&#39;m running JDK 15 (Just downloaded it) and I&#39;m getting a compile error when I try to use records:

    public class Runner {
    
    	// This gives a compile error
        record Point(int x, int y) {}
    
    }

Here is my pom.xml:

    &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
    &lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot;
             xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
             xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
        &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
    
        &lt;groupId&gt;something&lt;/groupId&gt;
        &lt;artifactId&gt;other-thing&lt;/artifactId&gt;
        &lt;version&gt;1.0.0-SNAPSHOT&lt;/version&gt;
    
        &lt;properties&gt;
            &lt;java.version&gt;15&lt;/java.version&gt;
        &lt;/properties&gt;
    
        &lt;build&gt;
            &lt;plugins&gt;
                &lt;plugin&gt;
                    &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                    &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
                    &lt;version&gt;3.8.1&lt;/version&gt;
                    &lt;configuration&gt;
                        &lt;source&gt;${java.version}&lt;/source&gt;
                        &lt;target&gt;${java.version}&lt;/target&gt;
                    &lt;/configuration&gt;
                &lt;/plugin&gt;
            &lt;/plugins&gt;
        &lt;/build&gt;
    
    &lt;/project&gt;

</details>


# 答案1
**得分**: 3

记录在Java 15中仍然是一个预览功能,因此在运行编译器时需要添加`--enable-preview`参数:

```xml
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.8.1</version>
    <configuration>
        <source>${java.version}</source>
        <target>${java.version}</target>
        <compilerArgs>
            <arg>--enable-preview</arg>
        </compilerArgs>
    </configuration>
</plugin>
英文:

Records are still a preview feature in Java 15, so you will need to add the --enable-preview argument when running the compiler:

&lt;plugin&gt;
    &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
    &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
    &lt;version&gt;3.8.1&lt;/version&gt;
    &lt;configuration&gt;
        &lt;source&gt;${java.version}&lt;/source&gt;
        &lt;target&gt;${java.version}&lt;/target&gt;
        &lt;compilerArgs&gt;
            &lt;arg&gt;--enable-preview&lt;/arg&gt;
        &lt;/compilerArgs&gt;
    &lt;/configuration&gt;
&lt;/plugin&gt;

huangapple
  • 本文由 发表于 2020年9月29日 07:25:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/64110887.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定