尝试fix method resolution in standalone java parser我发现了我不理解的a code in mockito。如果我要基于模拟代码创建小型测试:package org.mockitousage.matchers;import java.util.Collection;public class MoreMatchersTest { public interface IMethods { String simpleMethod(String argument); String simpleMethod(Collection<?> collection); String simpleMethod(Object argument); } private IMethods mock; public static <T> T any() { return null; } public static <T> T verify(T m) { return m; } public void any_should_be_actual_alias_to_anyObject() { verify(mock).simpleMethod(any()); }}如我所料,我收到编译错误:Error:(23, 21) java: reference to simpleMethod is ambiguous both method simpleMethod(java.lang.String) in org.mockitousage.matchers.MoreMatchersTest.IMethods and method simpleMethod(java.util.Collection<?>) in org.mockitousage.matchers.MoreMatchersTest.IMethods match但是不知何故,mockito编译成功。您能否解释一下为什么在嘲笑中编译代码(以及<T>any编译器选择哪种专业)或如何修改示例,使其也可以编译(我知道我可以将any()强制转换为特定类型或删除,但我希望尽可能接近模仿代码)。命令行输出:> javac -versionjavac 1.8.0_111> javac -d C:\w\MockitoTest\out\production\MockitoTest -classpath C:\w\MockitoTest\out\production\MockitoTest -sourcepath C:\w\MockitoTest\src -g -source 8 -target 8 C:\w\MockitoTest\src\org\mockitousage\matchers\MoreMatchersTest.javaC:\w\MockitoTest\src\org\mockitousage\matchers\MoreMatchersTest.java:23: error: reference to simpleMethod is ambiguous verify(mock).simpleMethod(any()); ^ both method simpleMethod(String) in IMethods and method simpleMethod(Collection<?>) in IMethods match1 error> javac -d C:\w\MockitoTest\out\production\MockitoTest -classpath C:\w\MockitoTest\out\production\MockitoTest -sourcepath C:\w\MockitoTest\src -g -source 7 -target 7 C:\w\MockitoTest\src\org\mockitousage\matchers\MoreMatchersTest.javawarning: [options] bootstrap class path not set in conjunction with -source 1.71 warning (adsbygoogle = window.adsbygoogle || []).push({}); 最佳答案 因此,从头开始,Java 8定义了类型详细说明here的类型推断规则,以使某些在1.7或1.6下愉快地编译的代码不再在1.8之下编译,例如,示例代码将不再编译确实:public class MoreMatchersTest { public interface IMethods { String simpleMethod(String argument); String simpleMethod(Collection<?> collection); String simpleMethod(Object argument); } private IMethods mock; public static <T> T any() { return null; } public static <T> T verify(T m) { return m; } public void any_should_be_actual_alias_to_anyObject() { verify(mock).simpleMethod(any()); }}关于此有多个问题,例如,请参见here和here。所以问题是为什么Mockito似乎可以工作,答案很简单。 Mockito 2.x发行版使用targetCompatibility = 1.6(github)进行编译,甚至Mockito母版上的某些模块也使用语言级别1.6。 (adsbygoogle = window.adsbygoogle || []).push({}); 09-11 04:52