我遇到一个奇怪的问题,方法引用Thread::sleep是模糊的,但具有相同签名的方法不是.
package test;
public class Test
{
public static void main(String[] args)
{
foo(Test::sleep, 1000L); //fine
foo((FooVoid<Long>)Thread::sleep, 1000L); //fine
foo(Thread::sleep, 1000L); //error
}
public static void sleep(long millis) throws InterruptedException
{
Thread.sleep(millis);
}
public static <P, R> void foo(Foo<P, R> function, P param) {}
public static <P> void foo(FooVoid<P> function, P param) {}
@FunctionalInterface
public interface Foo<P, R> {
R call(P param1) throws Exception;
}
@FunctionalInterface
public interface FooVoid<P> {
void call(P param1) throws Exception;
}
}
Run Code Online (Sandbox Code Playgroud)
我得到了这两个错误: …
我遇到了奇怪的错误消息,我认为这可能是不正确的。考虑以下代码:
public class Overloaded {
public interface Supplier {
int get();
}
public interface Processor {
String process(String s);
}
public static void load(Supplier s) {}
public static void load(Processor p) {}
public static int genuinelyAmbiguous() { return 4; }
public static String genuinelyAmbiguous(String s) { return "string"; }
public static int notAmbiguous() { return 4; }
public static String notAmbiguous(int x, int y) { return "string"; }
public static int strangelyAmbiguous() { return 4; }
public static String strangelyAmbiguous(int …Run Code Online (Sandbox Code Playgroud) java overloading arity functional-interface method-reference
我有以下类与重载方法:
import java.util.ArrayList;
import java.util.concurrent.Callable;
public abstract class Test {
public void test1 () {
doStuff (ArrayList::new); // compilation error
}
public void test2 () {
doStuff ( () -> new ArrayList<> ());
}
public abstract void doStuff (Runnable runable);
public abstract void doStuff (Callable<ArrayList<String>> callable);
}
Run Code Online (Sandbox Code Playgroud)
该方法test1导致错误消息的编译错误
The method doStuff(Runnable) is ambiguous for the type Test.
我添加了第三种方法test3,如下所示:
public void test3 () {
doStuff ( () -> {
new ArrayList<> ();
});
}
Run Code Online (Sandbox Code Playgroud)
这里doStuff(Runnable) …
我刚刚发现您可以在单个类型参数绑定中指定多个类型(请参阅示例).像任何新工具一样,我一直在尝试探索如何使用(和误用)的可能性.我精心设计了这个例子以帮助说明.
在下面的示例中,编译器给出了一个错误
dispatch(new AlphabetSoup());
对于Demo类型,方法dispatch(Demo.Soup)是不明确的
我能理解这一点,因为任何方法签名都匹配.我的问题是如何在不改变方法的情况下解决这个问题?如果我想要强制调用Soup版本,我可以将其转发给Soup:
dispatch((Soup)new AlphabetSoup())
但我不确定你是如何强制拨打其他版本的.可能吗?
public class Demo {
interface HasA { public char getA(); }
interface HasB { public char getB(); }
interface HasC { public char getC(); }
interface Soup {
public void eat();
}
class Alphabet implements HasA, HasB, HasC {
public char getA() { return 'a'; }
public char getB() { return 'b'; }
public char getC() { return 'c'; }
}
class AlphabetSoup implements Soup, HasA, HasB, HasC {
public void …Run Code Online (Sandbox Code Playgroud) 我使用org.apache.commons.lang3.BooleanUtils的commons-lang3(3.1版本).当我尝试编译下一行代码时
BooleanUtils.xor(true, true);
Run Code Online (Sandbox Code Playgroud)
使用maven-compiler-plugin(版本3.3),我收到编译失败消息:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.3:compile (default-compile) on project exchange: Compilation failure
[ERROR] MyClass.java:[33,34] reference to xor is ambiguous, both method xor(boolean...) in org.apache.commons.lang3.BooleanUtils and method xor(java.lang.Boolean...) in org.apache.commons.lang3.BooleanUtils match
Run Code Online (Sandbox Code Playgroud)
我使用Java 1.7.0_55进行编译.
我怎么解决这个问题?