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,.new

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

 

.new表達的是與.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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM