类加载器有变化吗

类加载器有变化吗

本文介绍了Java 8接口/类加载器有变化吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现Java 1.7_51和Java 1.8_20之间存在一些困难。

I have found some difficult cautious between Java 1.7_51 and Java 1.8_20.

初始情况:

一个界面:

interface InterfaceA {
    public void doSomething();
}

两个班级:

public class ClassA implements InterfaceA {
    public void doSomething() {
        System.out.println("Hello World!");
    }
}

public class ClassB {
    public static void main(String[] args) {
        ClassA a = new ClassA();
        a.doSomething();
    }
}

接下来我用(Java 1.8)编译了类 - > javac * .java
编译完成后我删除了InterfaceA.java和InterfaceA.class文件。
现在我再次尝试只编译ClassB.java并收到错误消息:

Next i have compiled the classes with (Java 1.8) -> javac *.javaafter the compiler finished i removed the InterfaceA.java and InterfaceA.class file's.Now i try again too compile only the ClassB.java and got the error message:

我尝试使用java 1.7 .. - > javac * .java
编译完成后我删除了InterfaceA.java和InterfaceA.class文件。
但我知道我没有收到任何错误消息..

The same i have tried with java 1.7.. -> javac *.javaafter the compiler finished i removed the InterfaceA.java and InterfaceA.class file's.But know i got no error message ..

有人可以解释一下吗?

。抱歉我的英文不好..

.. sorry for my bad english ..

推荐答案

正式规范描述了首先找到调用表达式的目标方法的过程搜索所有适用的方法,然后选择最具体的方法,如果没有歧义,则成功。

The formal specification describes the process of finding the target method of an invocation expression as first searching all applicable methods and then selecting the most specific one, succeeding if there is no ambiguity.

比较

在您的情况下,可以推断出在<$中找到的方法c $ c> ClassA 是一个完全匹配,编译器无法在 InterfaceA 中找到更具体的方法,但是,规范没有要求编译器必须在此时停止,使搜索短路。这是编译器可能具有的优化,但实现搜索就像正式指定一样,即首先搜索整个类型层次结构然后选择,这是合适的。

In your case it is possible to deduce that the method found in ClassA is an exact match for which the compiler can’t find a more specific method in InterfaceA, however, the specification does not mandate that the compiler has to stop at this point, short-circuiting the search. That’s an optimization a compiler might have, but implementing the search just like formally specified, i.e. searching the entire type hierarchy first and choosing then, is appropriate.

考虑到如何微妙和复杂的过程是所有新的Java 8特性和类型推断,可以理解当前的实现更保守而不是优化。

Given how subtle and complex the process is with all the new Java 8 features and type inference, it is understandable that the current implementation is more conservative rather that optimized.

这篇关于Java 8接口/类加载器有变化吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 10:08