【发布时间】:2017-08-20 14:45:43
【问题描述】:
偶然发现了这个网站:http://resources.mpi-inf.mpg.de/d5/teaching/ss05/is05/javadoc/java/io/FileNotFoundException.html
FileNotFoundException 类定义了三个构造函数:
FileNotFoundException()
Constructs a FileNotFoundException with null as its error detail message.
FileNotFoundException(String s)
Constructs a FileNotFoundException with the specified detail message.
private FileNotFoundException(String path, String reason)
Constructs a FileNotFoundException with a detail message consisting of the given pathname string followed by the given reason string.
但是最后一个构造函数被定义为私有的?
同样,在这里:http://www.docjar.com/html/api/java/io/FileNotFoundException.java.html 我们可以看到完整的类定义。没有其他代码,所以单例模式显然没有用于那种情况,我们也看不到,为什么要防止在对象之外实例化类,也不是工厂方法,静态(实用程序类)方法或仅常量类。
我是 C# 开发人员,所以我可能不知道这里发生的一些事情,但我仍然会对为什么将其定义为私有、使用什么以及是否有任何示例或用例感兴趣最后一个构造函数。
评论中提到:
此私有构造函数仅由本机 I/O 方法调用。
有人详细解释一下吗?
【问题讨论】:
-
好吧,还有什么要进一步解释的?显然他们决定 Java 代码不应该使用这个特定的构造函数。原因很难追查。
标签: java constructor private