java 內部類

來源:互聯網
上載者:User

標籤:com   system   out   nts   string   image   存取控制   常用   test   

今天主要學習了三個內部類

分別是:

1.成員內部類(member inner class)

不對外開開放,高內聚

package memberinner;public class Test {    // B類是 test.class 中的一個內部類    class B {        public void go() {            System.out.println("哈哈,你不知道的秘密!!!");        }    }        public void show() {        B b = new B();        b.go();    }        public static void main(String[] args) {                new Test().show();    }}

運行後會在你的項目的 bin 檔案中產生內部類檔案的標識。

 

2.方法內部類(method inner class)

  A.方法內部類就是內部類定義在外部類的方法中,方法內部類只在該方法的內部可見,即只在該方法內可以使用。

  B.方法內部類使用時,在方法中建立內部類對象,用對象調用其內部類中的方法。

  註:由於方法內部類不能在外部類的方法以外的地方使用呢,因此方法內部類不能使用存取控制符合 static 修飾符

 簡單來說,就是暫時性的一種,局部的。來看栗子:

package methodinner;public class Test {        public void driver() {        System.out.println("我在開車");        //System.out.println("撞死人了");//如何使你要負責的車禍變成沒事        //調用 DuanZanShiChangZheng.class 中的 noProblem 方法        class B extends DuanZanShiChangZheng {                    }        new B().noProblem();        System.out.println("最後,我莫事了,有病,就是這麼任性!!");    }        public static void main(String[] args) {        new Test().driver();    }    }

 

3.匿名內部類(anonymous inner class)

匿名內部類也就是沒有名字的內部類

正因為沒有名字,所以匿名內部類只能使用一次,它通常用來簡化代碼編寫

但使用匿名內部類還有個前提條件:必須繼承一個父類或實現一個介面

我們可以通過下面這個栗子,能夠明白。

package anonymousInner;public abstract class Doctor implements Qinshou{            public void workInDay() {        System.out.println("白天傳授理論知識。");    }    }
package anonymousInner;public interface Qinshou {        public void workInNight();}
package anonymousInner;public class Doctor1 extends Doctor{    public void workInNight() {        System.out.println("晚上就教Hibernate");    }}
package anonymousInner;public class Doctor2 extends Doctor{    public void workInNight() {                System.out.println("晚上,月黑風高,我們要全心學習Android");    }}
package anonymousInner;public class Test {    public static void main(String[] args) {        Doctor1 d1 = new Doctor1();        d1.workInDay();        d1.workInNight();                Doctor2 d2 = new Doctor2();        d2.workInDay();        d2.workInNight();                /**         * 匿名內部類 文法三步走:         * 1.new 一個抽象類別 或者 介面         * 2.加上一個花括弧         * 3.給它添加為實現的方法                 */                Doctor d3 = new Doctor() {            public void workInNight() {                System.out.println("我是doctor3 ,我們晚上要學習oracle");            }        };        d3.workInDay();        d3.workInNight();    }}

 

java 內部類

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.