java內部類對象使用.this,.new

public class InnerClass {

    class Content {
        private int i;

        public int getVlaue() {
            return i;
        }

    }


    class Description {

        private String lable;

        Description(String lab) {
            this.lable = lab;

        }

        public String readLable() {
            return lable;
        }
    }

    public Content getContentInstance() {

        return new Content();
    }

    public Description getDescriptionIntance(String lable) {

        return new Description(lable);
    }

    public void ship(String lable) {

        Content content = getContentInstance();

        Description description = getDescriptionIntance(lable);

        System.out.println(description.readLable());


        System.out.println(description.readLable());


    }

    /**
     * 在建立外部類對象前,是不能夠建立內部類對象的,由於內部類對象會暗暗的鏈接到外部類對象之上。<p>
     * 若是你想經過外圍類對象建立內部類對象  以前已經說過最簡單的方法是在外圍類聲明一個方法指向其內部類的對象。另外一種更加簡單的作法
     * JAVA支持經過外圍類對象.new 語法表達一個外圍類對象的引用
     * @param args
     */
    public static void main(String[] args) {

        InnerClass parcle2 = new InnerClass();

        InnerClass.Content c = parcle2.new Content();
        System.out.println(c.getVlaue());
        // parcle2.ship("hi");
        // InnerClass.Content c = parcle2.getContentInstance();// 若是想在外部類的非靜態方法以外的任意位置訪問某個內部類的對象,那麼必須經過OutClass.xx

        InnerClass.Description d = parcle2.new Description("hello");
        System.out.println(d.readLable());
        // InnerClass.Description d = parcle2.getDescriptionIntance("hello");
    }


}
View Code

使用.this,.newide

.this  表達的是在內部類對象域內  經過外部類.this 指向了一個在內部類指向外圍類對象引用的關係。只有這樣能夠訪問外圍類對象的屬性與方法this

 

.new表達的是與.this方向相反  當在外圍類做用域上 想建立內部類對象  以前通用的作法是 在外圍類建立一個指向內部類的引用來建立內部類,但有一種更加快捷的方式spa

直接外圍類.new  就能夠表達一個外圍類對象引用 。這裏必須強調一點  在擁有外部類對象以前是不可能建立外圍類對象的,由於內部類對象會暗暗的鏈接到建立他的外圍類對象上code

改變一下上面的內部類例子對象

public class InnerClass {

    class Content {
        private int i;

        public int getVlaue() {
            return i;
        }

    }


    class Description {

        private String lable;

        Description(String lab) {
            this.lable = lab;

        }

        public String readLable() {
            return lable;
        }
    }

    public Content getContentInstance() {

        return new Content();
    }

    public Description getDescriptionIntance(String lable) {

        return new Description(lable);
    }

    public void ship(String lable) {

        Content content = getContentInstance();

        Description description = getDescriptionIntance(lable);

        System.out.println(description.readLable());


        System.out.println(description.readLable());


    }

    /**
     * 在建立外部類對象前,是不能夠建立內部類對象的,由於內部類對象會暗暗的鏈接到外部類對象之上。<p>
     * 若是你想經過外圍類對象建立內部類對象  以前已經說過最簡單的方法是在外圍類聲明一個方法指向其內部類的對象。另外一種更加簡單的作法
     * JAVA支持經過外圍類對象.new 語法表達一個外圍類對象的引用
     * @param args
     */
    public static void main(String[] args) {

        InnerClass parcle2 = new InnerClass();

        InnerClass.Content c = parcle2.new Content();
        System.out.println(c.getVlaue());
        // parcle2.ship("hi");
        // InnerClass.Content c = parcle2.getContentInstance();// 若是想在外部類的非靜態方法以外的任意位置訪問某個內部類的對象,那麼必須經過OutClass.xx

        InnerClass.Description d = parcle2.new Description("hello");
        System.out.println(d.readLable());
        // InnerClass.Description d = parcle2.getDescriptionIntance("hello");
    }


}
View Code
相關文章
相關標籤/搜索