我几乎可以肯定有人问过这个问题,但我不确定要搜索什么。
无论如何,我很好奇是否可以创建一个扩展类ByteBuffer
。我认为这是不可能的,因为ByteBuffer
有包私有的构造函数:
// package-private
ByteBuffer(int mark, int pos, int lim, int cap, byte[] hb, int offset) {
super(mark, pos, lim, cap);
this.hb = hb;
this.offset = offset;
}
// Creates a new buffer with the given mark, position, limit, and capacity
//
ByteBuffer(int mark, int pos, int lim, int cap) { // package-private
this(mark, pos, lim, cap, null, 0);
}
但是,我发现如果您在与其父级共享名称的包中创建您的类,那么它可以完美编译。
package java.nio;
public class Test extends ByteBuffer {
Test(int mark, int pos, int lim, int cap, byte[] hb, int offset) {
super(mark, pos, lim, cap, hb, offset);
}
@Override
public ByteBuffer slice() {
return null;
}
...
}
它也可以在 Java 9 和 Java 10 中编译,但仅--patch-module
在编译时使用:
javac --patch-module java.base=. java/nio/Test.java
我的问题是:这是如何(以及为什么)编译的?