关于AbstractList的jdk源代码

时间:2018-03-26 14:41:10

标签: java

我正在阅读jdk源代码,发现一些问题: 为什么没有new的Object可以使用非静态的方法?

class SubList<E> extends AbstractList<E> {
private final AbstractList<E> l;
private final int offset;
private int size;

SubList(AbstractList<E> list, int fromIndex, int toIndex) {
    if (fromIndex < 0)
        throw new IndexOutOfBoundsException("fromIndex = " + fromIndex);
    if (toIndex > list.size())
        throw new IndexOutOfBoundsException("toIndex = " + toIndex);
    if (fromIndex > toIndex)
        throw new IllegalArgumentException("fromIndex(" + fromIndex +
                                           ") > toIndex(" + toIndex + ")");
    l = list;
    offset = fromIndex;
    size = toIndex - fromIndex;
    this.modCount = l.modCount;
}

public E set(int index, E element) {
    rangeCheck(index);
    checkForComodification();
    return l.set(index+offset, element);
}

public E get(int index) {
    rangeCheck(index);
    checkForComodification();
    return l.get(index+offset);
}

为什么list.size()和l.get()措辞?

1 个答案:

答案 0 :(得分:0)

size()和get()确实是AbstractList的非静态方法。 Proc sql noprint; create table want as select a.* from A as a inner join B as b on a.part_code = b.part_code and a.city = b.city; quit; list.size()正常工作,因为它们是从AbstractList实例(l.get()list)正确调用的。该实例作为l的构造函数的参数提供,然后存储在类的私有字段中:

SubList

private final AbstractList<E> l; [...] SubList(AbstractList<E> list, int fromIndex, int toIndex) { [...] l = list; 的实例必须在某个时刻创建(可能带有AbstractList),然后再将其作为参数传递给new的构造函数。