Java 8接口/类加载器有变化吗? [英] Java 8 interface/class loader changes?

查看:436
本文介绍了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 *.java after 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:


ClassB.java:4:error :无法访问InterfaceA
a.doSomething();

未找到InterfaceA的类文件
1错误

ClassB.java:4: error: cannot access InterfaceA a.doSomething();
class file for InterfaceA not found 1 error

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

The same i have tried with java 1.7.. -> javac *.java after 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.

比较 JLS 15.12.2.1。确定可能适用的方法


搜索编译时步骤1(第15.12.1节)确定的类或接口所有可能适用于此方法调用的成员方法;从超类和超接口继承的成员包含在此搜索中。

The class or interface determined by compile-time step 1 (§15.12.1) is searched for all member methods that are potentially applicable to this method invocation; members inherited from superclasses and superinterfaces are included in this search.

在您的情况下,可以推断出在<$中找到的方法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接口/类加载器有变化吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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