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

來源:互聯網
上載者:User

標籤:get   param   外部   類對象   通用   esc   closed   src   屬性   

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

 

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.