java.nio.file.Files.newByteChannel
返回SeekableByteChannel
。但是返回的对象的实际类是sun.nio.ch.FileChannelImpl
(在我的 Java 中),所以我可以将它转换java.nio.channels.FileChannel
为 FileChannel 并将其用作 FileChannel。有谁知道Files.newByteChannel
不能退货的原因FileChannel
吗?
问问题
529 次
1 回答
1
有人知道 Files.newByteChannel 无法返回 FileChannel 的原因吗?
FileChannel
(抽象类)实现SeekableByteChannel
接口。
一般来说,使用类而不是类总是好的做法,object reference of interface
因为如果我们更改实现,那么我们不需要在任何地方更改代码。
于 2012-12-19T10:23:40.323 回答