天天看點

java匿名内部類

show the code :

package com.test.jwen.httpApiAuto;

public class AInter {

     public void m0(){

          System.out.println("1");

     }

     public static void main(String[] args){

          new AInter().m0();

          new AInter(){

              public void m0(){

                   System.out.println("2");

              }

          }.m0();

}

匿名内部類,就是沒有名字的類,在運作的時候,會提示裡面有兩個類,一個AInter一個AInter$1,這個AInter$1就是這個匿名内部類

例子中重寫了m0方法,一般用來簡化代碼編寫,但個人覺得可讀性降低了

使用前提:必須繼承一個父類 或者 實作 一個接口

http://www.cnblogs.com/nerxious/archive/2013/01/25/2876489.html

雖千萬人,吾往矣!