- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
考虑这个接口(interface):
@FunctionalInterface
public interface ThrowingFunction<T, R>
{
R tryApply(T t)
throws Throwable;
}
这个类:
public final class Wrapper<T, R>
{
private final ThrowingFunction<T, R> f;
public Wrapper(final ThrowingFunction<T, R> f)
{
this.f = f;
}
}
有了这些,我可以写:
final ThrowingFunction<Path, Path> f = Path::toRealPath;
new Wrapper(f);
但我不能写:
new Wrapper(Path::toRealPath);
问题是由于the method prototype .它可以将 LinkOption...
作为参数。
然而,编译器能够“强制”此处的方法引用。我想出的唯一模仿这个的方法是彻头彻尾的骇人听闻:
@FunctionalInterface
public interface ThrowingFunction<T, R>
{
R tryApply(T t)
throws Throwable;
@FunctionalInterface
interface WithVarags<T, V, R>
extends ThrowingFunction<T, R>
{
R tryApplyWithVarargs(T t, V... ignored)
throws Throwable;
@Override
default R tryApply(T t)
throws Throwable
{
return tryApplyWithVarargs(t);
}
}
}
然后向Wrapper
添加一个新的构造函数:
public <V> Wrapper(
final ThrowingFunction.WithVarags<T, V, R> function)
{
this.function = function;
}
有没有更简洁的方法来做到这一点?也就是说,在这种情况下我可以“像编译器一样”吗?
最佳答案
您可以在实例化时指定 Wrapper
的类型。
new Wrapper<Path, Path>(Path::toRealPath);
或
Wrapper<Path, Path> wrapper = new Wrapper<>(Path::toRealPath);
我认为这与可变参数无关。通过指定类型,您还告诉编译器“忽略”varargs 参数。
请注意,我看到您也可以“忽略”同一函数的返回类型:
ThrowingBiConsumer<Path, LinkOption[]> biConsumer = Path::toRealPath;
new Wrapper<Path, LinkOption[]>(Path::toRealPath);
public interface ThrowingBiConsumer<T, U> {
void accept(T t, U u) throws Throwable;
}
关于java - 在@FunctionalInterface 中处理可变参数的更简洁方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27723325/
这个问题在这里已经有了答案: Should 'Comparable' be a 'Functional interface'? (4 个答案) 关闭 5 年前。 Comparable 和 Compa
尝试编译 Java 时 @FunctionalInterface具有超过 1 个非抽象方法会引发编译错误。 但是,在 Kotlin 中执行相同操作时,不会引发错误或警告,即以下 Kotlin inte
我正在尝试创建一个可以抛出自定义异常的函数式接口(interface),我的想法是。 public class MyException extends Exception { public M
我正在学习将一些 lambda 表示形式写成 FunctionalInterface .所以,要添加我使用的两个整数: BiFunction biFunction = (a, b) -> a + b;
简介 在搜索网络时,我偶然发现了 Benoit Tellier 的博客文章,Next level Java 8 staged builders ,他在其中分享了针对某些用例的分阶段构建器模式的变体。
假设我有以下界面: @FunctionalInterface public interface First { int fun(int a); } 还有 @FunctionalInterface
为什么我不能使用默认方法实现创建 @FunctionalInterface? @FunctionalInterface public interface MyInterface { defau
下面的接口(interface)是 Java 8 中的有效功能接口(interface)吗? @FunctionalInterface interface Normal{ public abs
Said in Javadoc : If a type is annotated with this annotation type, compilers are required to genera
考虑这个接口(interface): @FunctionalInterface public interface ThrowingFunction { R tryApply(T t)
在我的库中,我有一个简单的适配器接口(interface)可以注册。 Adapter firstAdapter = new FirstAdapter(String someParam); Adapte
Said in Javadoc : If a type is annotated with this annotation type, compilers are required to genera
在 Java 8 中,引入了接口(interface)的默认方法,以便在不破坏向后兼容性的情况下向现有接口(interface)添加方法。 由于默认方法是非抽象的,因此它们可用于制作具有多个可覆盖方法
我最初的问题与 this one 完全相同;也就是说,为什么这个接口(interface)有运行时保留策略。 但是接受的答案根本不让我满意,原因有二: 这个接口(interface)是 @Docume
我一直在学习 Java 8 的特性,到目前为止已经成功实现了它们,但我最新的代码引发了两个问题。 第一个问题可以通过强制转换来解决,但是这里不应该发生这种情况,因为类型继承应该是健全的;这是在接受单参
学习 Java 8 Lambda 并想知道编译器如何知道 Comparator 中的哪个方法用于 lambda 表达式?好像不是SAM界面?它有 2 个抽象方法: @FunctionalInterfa
学习 Java 8 Lambda,只是想知道编译器如何知道 Comparator 中的哪个方法用于 lambda 表达式?好像不是SAM接口(interface)?它有 2 个抽象方法: @Funct
我刚开始学习 Camel ,我看到的第一件事是 context.addRoutes(new RouteBuilder() { public void configure() {
Callable 会抛出异常,而 Runnable 不会。 有没有标准的样子 @FunctionalInterface public interface TypedBlock { public
这个问题在这里已经有了答案: FunctionalInterface Comparator has 2 abstract methods (3 个答案) 关闭 3 年前。 我想澄清一下我对@Func
我是一名优秀的程序员,十分优秀!