带有 -source 1.7 的 JDK8 [默认方法] [英] JDK8 with -source 1.7 [Default Methods]

查看:27
本文介绍了带有 -source 1.7 的 JDK8 [默认方法]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下课程.

public class ZonedDateTimeToInstant {

    public static void main(final String[] args)
        throws NoSuchMethodException {

        assert ChronoZonedDateTime.class.isAssignableFrom(ZonedDateTime.class);
        final Method toInstant
            = ChronoZonedDateTime.class.getMethod("toInstant");

        final ZonedDateTime now = ZonedDateTime.now();
        final Instant instant = now.toInstant();

        System.out.println(instant);
    }
}

它编译得很好.

& javac ZonedDateTimeToInstant.java

它以 -source 1.7 失败.

& javac -source 1.7 ZonedDateTimeToInstant.java
ZonedDateTimeToInstant.java:10: error: cannot find symbol
    final Instant instant = now.toInstant();
                               ^
  symbol:   method toInstant()
  location: variable now of type ZonedDateTime
1 error
1 warning

这正常吗?javac 似乎不理解带有 -source 的 JDK 类,而不是 1.8.

Is this normal? It seems that javac doesn't understand JDK classes with -source other than 1.8.

根据 javacjavac 仍然像以前的版本一样支持各种 -source release 选项.

According to javac, javac still supports various -source release options as previous releases did.

补充

我已经知道 JSR 310:日期和时间 API 仅可用在 Java 8 中.javac 有什么关系?

I already know the JSR 310: Date and Time API is only available in Java 8. What does it matter with javac?

$ cat Java8.java
public class Java8 {
    public void print(java.io.PrintStream out) {
        out.printf("hello world
");
    }
}
$ javac Java8.java
$ cat Java7.java
public class Java7 {
    public static void main(final String[] args) {
        new Java8().print(System.out);
    }
}
$ javac -source 1.7 -target 1.7 Java7.java
warning: [options] bootstrap class path not set in conjunction with -source 1.7
1 warning
$ java Java7
hello world

结论

正如@Eng.Fouad 指出的那样.问题在于该方法是在接口中定义的default method.javac 似乎抓住了这一点.

As @Eng.Fouad noted. The problem was that the method is a default method defined in an interface. javac seems to be catching that point.

$ cat Java8i.java
public interface Java8i {
    default void print(java.io.PrintStream out) {
        out.printf("hello world
");
    }
}
$ javac Java8i.java
$ cat Java8c.java
public class Java8c implements Java8i {
}
$ javac Java8c.java
$ cat Java7i.java
public class Java7i {
    public static void main(final String[] args) {
        new Java8c().print(System.out);
    }
}
$ javac -source 1.7 -target 1.7 Java7i.java
warning: [options] bootstrap class path not set in conjunction with -source 1.7
Java7i.java:3: error: cannot find symbol
    new Java8c().print(System.out);
                ^
  symbol:   method print(PrintStream)
  location: class Java8c
1 error
1 warning

javac 应该告诉我更有帮助.

javac should've told me more helpfully.

推荐答案

这是 Java 8 中引入的一个新的时间/日期 API.这就是它不能与 Java 7 一起编译的原因.

toInstant() 是默认方法,而 -source 1.7 不支持默认方法(Java 8 的新特性).

toInstant() is a default method, whereas -source 1.7 doesn't support default methods (Java 8's new feature).

这篇关于带有 -source 1.7 的 JDK8 [默认方法]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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