Java內部類基本使用

分類:技術 時間:2017-01-20

鏈接到外部類

??創建內部類時,那個類的對象同時擁有封裝對象(封裝內部類的外部類)的一個鏈接,所以內部類可以訪問外部類的成員。
??內部類擁有對外部類所有元素的訪問權限。
??看如下代碼,內部類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);
    }
}




Tags: java 內部類 java

文章來源:


ads
ads

相關文章
ads

相關文章

ad