gpt4 book ai didi

java - Scala/Java语法,返回接口(interface)实现

转载 作者:行者123 更新时间:2023-11-29 07:50:13 26 4
gpt4 key购买 nike

这行代码在最下面的代码中是什么意思?

return Promise.<SimpleResult>pure  //why is the dot

我是 Scala 和 Java 6 的新手,相关主题/术语是什么?

如果框架没有找到请求的操作方法,onHandlerNotFound操作将被调用:

import play.*;
import play.mvc.*;
import play.mvc.Http.*;
import play.libs.F.*;

import static play.mvc.Results.*;

public class Global extends GlobalSettings {

public Promise<SimpleResult> onHandlerNotFound(RequestHeader request) {
return Promise.<SimpleResult>pure(notFound(
views.html.notFoundPage.render(request.uri())
));
}

更新:

play.libs.F.Java文件

public static class Promise<A> {

public static <A> Promise<A> pure(final A a) {
return FPromiseHelper.pure(a);
}
}

什么是 <A>Promise<A>

最佳答案

Promise.pure()是一个泛型方法,用某种类型 T 参数化。

Promise.<SimpleResult>pure()使用 SimpleResult 作为通用类型调用此方法。大多数时候,这样做是可选的,因为编译器从方法的参数或结果分配给的变量的类型推断泛型类型,如

List<String> s = Collections.emptyList();

这是一个快捷方式

List<String> s = Collections.<String>emptyList();

Set<String> Collections.singleton("hello");

这是一个快捷方式

Set<String> Collections.<String>singleton("hello");

关于java - Scala/Java语法,返回接口(interface)实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21781048/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com