Java内部类基本使用

链接到外部类

  创建内部类时,那个类的对象同时拥有封装对象(封装内部类的外部类)的一个链接,所以内部类可以访问外部类的成员。
  内部类拥有对外部类所有元素的访问权限。
  看如下代码,内部类SSelector访问外部类元素o,而且o是private。

interface Selector
{
    boolean end();
    Object current();
    void next();
}

public class Sequence
{
    private Object[] o;
    private int next = 0;
    public Sequence(int size)
    {
        o = new Object[size];
    }

    public void add(Object x)
    {
        if(next < o.length)
        {
            o[next] = x;
            next++;
        }
    }

    private class SSelector implements Selector
    {
        int i = 0;
        public boolean end()
        {
            return i == o.length;
        }
        public Object current()
        {
            return o[i];
        }
        public void next()
        {
            if(i < o.length)
            {
                i++;
            }
        }
    }

    public Selector getSelector()
    {
        return new SSelector();
    }

    public static void main(String[] args)
    {
        Sequence s = new Sequence(10);
        for(int i = 0; i < 10; i++)
        {
            s.add(Integer.toString(i));
        }
        Selector s1 = s.getSelector();
        while (!s1.end())
        {
            System.out.println((String)s1.current());
            s1.next();
        }
    }
}

输出结果如下:

0
1
2
3
4
5
6
7
8
9

static内部类

  • 为创建一个static内部类的对象,不需要一个外部类对象。
  • 不能从static内部类的一个对象访问一个外部类对象。
  • 为创建内部类的对象而不需要创建外部类的一个对象,那么可将所有东西设置为static。
abstract class Contents
{
    abstract public int value();
}

interface Destination
{
    String readLabel();
}


public class Test3
{
    private static class PContents extends Contents
    {
        private int i = 11;
        public int value()
        {
            return i;
        }
    }

    protected static class PDestination implements Destination
    {
        private String label;
        private PDestination(String whereTo)
        {
            label = whereTo;
        }
        public String readLabel()
        {
            return label;
        }
    }

    public static Contents cont()
    {
        return new PContents();
    }

    public static Destination dest(String s)
    {
        return new PDestination(s);
    }

    public static void main(String[] args)
    {
        Contents c = cont();
        Destination d = dest("Wu Han");
        System.out.println(c.value());
        System.out.println(d.readLabel());
    }
}

内部类中引用外部类对象

  若想在内部类中生成外部类的句柄,就要用一个.和this来命名外部类。
  如下,第一次输出为Test3中的x,初始值为0,第二次使用内部类中的method方法对外部类x进行修改,使其变为5。

public class Test3
{
    int x = 0;

    public class Test4
    {
        int x;
        public void method()
        {
            //内部类x
            x = 3;
            //外部类
            Test3.this.x = 5;
        }
    }

    public Test4 test()
    {
        return new Test4();
    }

    public static void main(String[] args)
    {
        Test3 test3 = new Test3();
        Test4 test4 = test3.test();
        System.out.println(test3.x);
        test4.method();
        System.out.println(test3.x);
    }
}

输出结果:

0
5

通过外部类对象引用内部类对象

  通过外部类对象引用加上.和new与该外部类对应的内部类对象,就可以通过外部类对象来引用内部类对象。
  代码如下,整体与上述代码基本相同,就是在获取内部类对象的时候直接使用.new获取。输出结果也是0 5

public class Test3
{
    int x = 0;

    public class Test4
    {
        int x;
        public void method()
        {
            //内部类x
            x = 3;
            //外部类
            Test3.this.x = 5;
        }
    }

    public static void main(String[] args)
    {
        Test3 test3 = new Test3();
        Test4 test4 = test3.new Test4();
        System.out.println(test3.x);
        test4.method();
        System.out.println(test3.x);
    }
}

posted on 2017-01-20 16:59  liuyang0  阅读(207)  评论(0编辑  收藏  举报

导航

Flag Counter