JAVA的四种内部类及为什么要用内部类

        最近在看java的源码,但是时长能看一个类中都会有几个内部类比如LinkedList中
private class ListItr implements ListIterator<E> {
        private Node<E> lastReturned;
        private Node<E> next;
        private int nextIndex;
        private int expectedModCount = modCount;

        ListItr(int index) {
            // assert isPositionIndex(index);
            next = (index == size) ? null : node(index);
            nextIndex = index;
        }

        public boolean hasNext() {
            return nextIndex < size;
        }

        public E next() {
            checkForComodification();
            if (!hasNext())
                throw new NoSuchElementException();

            lastReturned = next;
            next = next.next;
            nextIndex++;
            return lastReturned.item;
        }

        public boolean hasPrevious() {
            return nextIndex > 0;
        }

        public E previous() {
            checkForComodification();
            if (!hasPrevious())
                throw new NoSuchElementException();

            lastReturned = next = (next == null) ? last : next.prev;
            nextIndex--;
            return lastReturned.item;
        }

        public int nextIndex() {
            return nextIndex;
        }

        public int previousIndex() {
            return nextIndex - 1;
        }

        public void remove() {
            checkForComodification();
            if (lastReturned == null)
                throw new IllegalStateException();

            Node<E> lastNext = lastReturned.next;
            unlink(lastReturned);
            if (next == lastReturned)
                next = lastNext;
            else
                nextIndex--;
            lastReturned = null;
            expectedModCount++;
        }

        public void set(E e) {
            if (lastReturned == null)
                throw new IllegalStateException();
            checkForComodification();
            lastReturned.item = e;
        }

        public void add(E e) {
            checkForComodification();
            lastReturned = null;
            if (next == null)
                linkLast(e);
            else
                linkBefore(e, next);
            nextIndex++;
            expectedModCount++;
        }

        public void forEachRemaining(Consumer<? super E> action) {
            Objects.requireNonNull(action);
            while (modCount == expectedModCount && nextIndex < size) {
                action.accept(next.item);
                lastReturned = next;
                next = next.next;
                nextIndex++;
            }
            checkForComodification();
        }

        final void checkForComodification() {
            if (modCount != expectedModCount)
                throw new ConcurrentModificationException();
        }
    }

    private static class Node<E> {
        E item;
        Node<E> next;
        Node<E> prev;

        Node(Node<E> prev, E element, Node<E> next) {
            this.item = element;
            this.next = next;
            this.prev = prev;
        }
    }

然后就开始想为什么java本身中要用内部类呢?

首先介绍下四种内部类

  • 成员内部类
  • 静态内部类
  • 局部内部类
  • 匿名内部类

一 成员内部类

1.没有使用static修饰的内部类。
2.在成员内部类中不允许出现静态变量和静态方法的声明。static只能用在静态常量的声明上。
3.成员内部类中可以访问外部类中所有的成员(变量,方法),包含私有成员,如果在内部类中定义有和外部类同名的实例变量,访问:OuterClass.this.outerMember;
4.构建内部类的实例,要求必须外部类的实例先存在
外部类的外部/外部类的静态方法:new Outer().new Inner();

外部类的实例方法:new Inner();this.new Inner();

二 静态内部类

1.声明在类体部,方法体外,并且使用static修饰的内部类
2.访问特点可以类比静态变量和静态方法
3.脱离外部类的实例独立创建
 在外部类的外部构建内部类的实例 new Outer.Inner();
 在外部类的内部构建内部类的实例 new Inner();

 4.静态内部类体部可以直接访问外部类中所有的静态成员,包含私有

三 局部内部类

 1.定义在方法体,甚至比方法体更小的代码块中
 2.类比局部变量。
 3.局部内部类是所有内部类中最少使用的一种形式。
 4.局部内部类可以访问的外部类的成员根据所在方法体不同。
 如果在静态方法中:可以访问外部类中所有静态成员,包含私有
 如果在实例方法中:可以访问外部类中所有的成员,包含私有。

 局部内部类可以访问所在方法中定义的局部变量,但是要求局部变量必须使用final修饰。

四 匿名内部类

 1.没有名字的局部内部类。
 2.没有class,interface,implements,extends关键字
 3.没有构造器。

 4.一般隐式的继承某一个父类或者实现某一个接口


介绍完四种内部类后来说下我理解的为什么要用内部类

1 起到更好的封装作用,不让你知道具体实现细节,神秘感很足,比如:

private static class Node<E> {
        E item;
        Node<E> next;
        Node<E> prev;

        Node(Node<E> prev, E element, Node<E> next) {
            this.item = element;
            this.next = next;
            this.prev = prev;
        }
    }

这个就是LinkedList中的内部类,也是链表结果的实现所需要的类

2 能巧妙躲过java中只能单继承的弊端,比如:

private class DescendingIterator implements Iterator<E> {
        private final ListItr itr = new ListItr(size());
        public boolean hasNext() {
            return itr.hasPrevious();
        }
        public E next() {
            return itr.previous();
        }
        public void remove() {
            itr.remove();
        }
    }
这个内部类+接口就很好的实现了多继承

猜你喜欢

转载自blog.csdn.net/weixin_41574643/article/details/79962615
今日推荐