我注意到在Java8和Java9中运行以下程序的输出不同。
你可以在这里运行两个版本的Java: https://www.jdoodle.com/online-java-compiler/ Java 8输出:
Java 9 输出:
我在想是什么导致了这个问题?是否有我不知道的新Java功能?
Java 8文档 https://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.4.8 Java 9文档 https://docs.oracle.com/javase/specs/jls/se9/html/jls-8.html#jls-8.4.8
import java.lang.reflect.Method;
public class OrderingTest {
public static void main(String[] args) {
ServiceImpl service = new ServiceImpl();
for (Method method : service.getClass().getMethods()) {
for (Class<?> anInterface : method.getDeclaringClass().getInterfaces()) {
try {
Method intfMethod = anInterface.getMethod(method.getName(), method.getParameterTypes());
System.out.println("intfMethod = " + intfMethod);
} catch (NoSuchMethodException e) { }
}
}
}
}
class ServiceImpl implements ServiceX {
@Override
public Foo getType() { return null; }
}
interface ServiceX extends ServiceA<Foo>, ServiceB { }
abstract class Goo { }
class Foo extends Goo { }
interface ServiceA<S> {
S getType();
}
interface ServiceB {
@java.lang.Deprecated
Goo getType();
}
你可以在这里运行两个版本的Java: https://www.jdoodle.com/online-java-compiler/ Java 8输出:
intfMethod = public abstract java.lang.Object ServiceA.getType()
intfMethod = public abstract java.lang.Object ServiceA.getType()
intfMethod = public abstract java.lang.Object ServiceA.getType()
Java 9 输出:
intfMethod = public abstract Goo ServiceB.getType()
intfMethod = public abstract Goo ServiceB.getType()
intfMethod = public abstract Goo ServiceB.getType()
但是当我重新排序超级接口为:
interface ServiceX extends ServiceB, ServiceA<Foo> { }
然后两个版本的Java都输出:
intfMethod = public abstract Goo ServiceB.getType()
intfMethod = public abstract Goo ServiceB.getType()
intfMethod = public abstract Goo ServiceB.getType()
我在想是什么导致了这个问题?是否有我不知道的新Java功能?
Java 8文档 https://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.4.8 Java 9文档 https://docs.oracle.com/javase/specs/jls/se9/html/jls-8.html#jls-8.4.8