ByteBuffer是个抽象类:
//4个
public abstract class Buffer {
private int mark = -1;
private int position = 0;
private int limit;
private int capacity;
}
//2个数据成员
public abstract class ByteBuffer
extends Buffer
implements Comparable<ByteBuffer>
{
final byte[] hb; // Non-null only for heap buffers
final int offset;
// boolean isReadOnly; // Valid only for heap buffers
}
public static ByteBuffer allocate(int capacity) {
if (capacity < 0)
throw new IllegalArgumentException();
return new HeapByteBuffer(capacity, capacity);//使用的是抽象基类,在基类中返回子类对象
}
class HeapByteBuffer extends ByteBuffer{
HeapByteBuffer(int cap, int lim) {//子类构造函数,调用基类构造函数构造成员
super(-1, 0, lim, cap, new byte[cap], 0);
/*
hb = new byte[cap];
offset = 0;
*/
/*
ByteBuffer(int mark, int pos, int lim, int cap, // package-private
byte[] hb, int offset)
{
super(mark, pos, lim, cap);
this.hb = hb;
this.offset = offset;
}
*/
/*
Buffer(int mark, int pos, int lim, int cap) { // package-private
if (cap < 0)
throw new IllegalArgumentException("Negative capacity: " + cap);
this.capacity = cap;
limit(lim);
position(pos);
if (mark >= 0) {
if (mark > pos)
throw new IllegalArgumentException("mark > position: ("
+ mark + " > " + pos + ")");
this.mark = mark;
}
}
*/
}
}