java - java中的自然返回类型是什么?

标签 java return return-value factory

我正在阅读 Joshua Bloch 的“Effective Java”,上面写着

A third advantage of static factory methods is that, unlike constructors, they can return an object of any subtype of their return type. This gives you great flexibility in choosing the class of the returned object. One application of this flexibility is that an API can return objects without making their classes public. Hiding implementation classes in this fashion leads to a very compact API. This technique lends itself to interface-based frameworks, where interfaces provide natural return types for static factory methods.

谁能解释什么是“自然返回类型”?谢谢!

最佳答案

在这种情况下,“自然”仅仅意味着工厂方法的上下文是自然的;即什么是合适的或你所期望的。直觉是另一个同义词。

这只是正常的英语用法……不是 IT 或 Java 特定的术语。

关于java - java中的自然返回类型是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54014437/

相关文章:

java - 当作为服务器运行时,Eclipse - Tomcat - java.lang.NoClassDefFoundError : javax/servlet/ServletConfig

java - 如何使用java将文件从url传输到ftp?

java - 在循环中使用 Map.put(String, Integer)

JavaScript for循环 "jump over value"

在 C 中将十进制转换为罗马 - 如何从函数返回字符串?

代码似乎在 return 语句后继续运行(在 C 中)

MATLAB:有关输出参数的脚本与函数

java - Spring Security - Ajax 调用忽略@ExceptionHandler

c - 线程终止后返回值错误

c - 将地址缓冲区从一个文件返回到另一个文件