内部类(一)创建内部类&链接到内部类

内部类

  • 可以将一个类的定义放在另一个类的定义内部,这就是内部类。
  • 内部类是一种非常有用的特性,因为它允许你把一些逻辑相关的类组织在一起,并控制位于内部的类的可视性。

创建内部类

创建内部类:把类的定义置于外围类的里面;

package com2001;

/**
 * Created by Panda on 2018/6/8.
 */
public class Parcel1 {
    class Contents{
        private int i=11;
        public int value(){return i;}
    }

    class Destination{
        private String label;
        public Destination(String label) {
            this.label = label;
        }
        String readLabel(){return label;}
    }

    public void ship(String dest){
        Contents contents = new Contents();
        Destination destination = new Destination(dest);
        System.out.println(destination.readLabel());
    }

    public static void main(String[] args) {
        Parcel1 parcel1=new Parcel1();
        parcel1.ship("milk");
    }
}
package com2001;

/**
 * Created by Panda on 2018/6/8.
 */
public class Parcel2 {
    class Contents{
        private int i=11;
        public int value(){return i;}
    }

    class Destination{
        private String label;
        public Destination(String label) {
            this.label = label;
        }
        String readLabel(){return label;}
    }

    public Destination to(String s){
        return new Destination(s);
    }
    public Contents contents(){
        return new Contents();
    }

    public void ship(String dest){
        Contents contents = contents();
        Destination destination =to(dest);
        System.out.println(destination.readLabel());
    }

    public static void main(String[] args) {
        Parcel2 parcel2=new Parcel2();
        parcel2.ship("milk");

        //内部类
        Parcel2 parcel21=new Parcel2();
        Parcel2.Contents contents = parcel21.contents();
        Parcel2.Destination destination = parcel21.to("milk1");
    }
}

如果想从外部类的非静态方法之外的任意位置创建某个内部类的对象,那么必须像在main()方法中那样,具体地指明这个对象的类型:OuterClassName.InnerClassName
这里写图片描述

链接到内部类

  • 当生成一个内部类的对象时,此对象与制造它的外围对象之间就有了一种联系,所以它能访问其外围对象的所有成员,而不需要任何特殊条件。此外,内部类还拥有其外围类的所有元素的访问权。
/**
 * Created by Panda on 2018/6/8.
 */
interface Selector{
    boolean end();
    Object current();
    void next();
}
public class Sequence {
    private Object[] items;
    private int next=0;
    public Sequence(int size){items=new Object[size];}
    public void add(Object x){
        if(next<items.length) items[next++]=x;
    }
    public class SequenceSelector implements Selector{
        private int i=0;
        @Override
        public boolean end() {
            return i==items.length;
        }

        @Override
        public Object current() {
            return items[i];
        }

        @Override
        public void next() {
            if(i<items.length) i++;
        }
    }

    public Selector selector(){
        return new SequenceSelector();
    }

    public static void main(String[] args) {
        Sequence sequence = new Sequence(10);
        for (int i = 0; i < 10; i++) {
            sequence.add(Integer.toString(i));
        }
        Selector selector = sequence.selector();
        while (!selector.end()){
            System.out.println(selector.current());
            selector.next();
        }
    }
}
  • 内部类自动拥有对其外围类所有成员的访问权。当某个外围类的对象创建了一个内部类对象时,此内部类对象必定会秘密地捕获一个指向那个外围类对象的引用。然后,在你访问此外围类的成员时,就是用那个引用来选择外围类的成员。
  • 内部类的对象只能与其外围类的对象相关联的情况下才能被创建。(内部类是非static类时)。构建内部类对象时,需要一个指向其外围类对象的引用。

猜你喜欢

转载自blog.csdn.net/Panda_____/article/details/80623521