ByteBuffer.allocate(int capacity)的理解

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;
				}
			}
			
			*/
    }

}

 

转载于:https://my.oschina.net/zengjs275/blog/1507405

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值