问题描述
用于 java的JavaDoc.io.FileDescriptor.FileDescriptor()说:
如果没有构造函数的目的,为什么没有声明其访问级别为 package-private ?
If there is no purpose for the constructor, why is it's access level not declared to be package-private?
推荐答案
此构造函数是公共的,因为它在 java.io
之外使用.
This constructor is public because it is used outside of java.io
.
在JRE 7u4 Linux x86中使用 new FileDescriptor()
的类:
Classes using new FileDescriptor()
in JRE 7u4 Linux x86:
java.io.FileInputStream
java.io.FileOutputStream
java.io.RandomAccessFile
java.lang.UNIXProcess
java.net.AbstractPlainDatagramSocketImpl
java.net.AbstractPlainSocketImpl
java.net.ServerSocket
sun.net.sdp.SdpSupport
sun.nio.ch.FileChannelImpl
sun.nio.ch.FileDispatcherImpl
sun.nio.ch.IOUtil
sun.nio.ch.PipeImpl
sun.nio.ch.SctpServerChannelImpl
sun.nio.ch.ServerSocketChannelImpl
sun.nio.ch.UnixAsynchronousServerSocketChannelImpl
sun.nio.fs.UnixChannelFactory
有一个 sun.misc.SharedSecrets
方法,允许程序员将 FileDescriptor
的状态更改为有效状态(此代码段在 java中找到.io.FileDescriptor
):
There is a sun.misc.SharedSecrets
method that allows the programmer to change the state of a FileDescriptor
to a valid one (this snippet found in java.io.FileDescriptor
):
static {
sun.misc.SharedSecrets.setJavaIOFileDescriptorAccess(
new sun.misc.JavaIOFileDescriptorAccess() {
public void set(FileDescriptor obj, int fd) {
obj.fd = fd;
}
public int get(FileDescriptor obj) {
return obj.fd;
}
public void setHandle(FileDescriptor obj, long handle) {
obj.handle = handle;
}
public long getHandle(FileDescriptor obj) {
return obj.handle;
}
}
);
}
这意味着任何可以访问 SharedSecrets
(即JRE本身)的代码也可以创建自己的有效 FileDescriptor
,因此应允许访问FileDescriptor()
.但是,没有办法将构造函数的访问限制为仅JRE类,因此它是公共的.
This means that any code that can access SharedSecrets
(I.E. the JRE itself) can also create its own valid FileDescriptor
, and should therefore be allowed to access FileDescriptor()
. However, there is no way to restrict the access of a constructor to only JRE classes, so it is public.
这篇关于为什么java.io.FileDescriptor的构造函数是公共的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!