匿名內部類也就是沒有名字的內部類
正因為沒有名字,所以匿名內部類只能使用一次,它通常用來簡化代碼編寫
但使用匿名內部類還有個前提條件:必須繼承一個父類或實現一個接口
實例1:不使用匿名內部類來實現抽象方
1 abstract class Person { 2 public abstract void eat(); 3 } 4 5 class Child extends Person { 6 public void eat() { 7 System.out.println("eat something"); 8 } 9 } 10 11 public class Demo { 12 public static void main(String[] args) { 13 Person p = new Child(); 14 p.eat(); 15 } 16 }
運行結果:eat something
可以看到,我們用Child繼承了Person類,然后實現了Child的一個實例,將其向上轉型為Person類的引用
但是,如果此處的Child類只使用一次,那么將其編寫為獨立的一個類豈不是很麻煩?
這個時候就引入了匿名內部類
實例2:匿名內部類的基本實現
1 abstract class Person { 2 public abstract void eat(); 3 } 4 5 public class Demo { 6 public static void main(String[] args) { 7 Person p = new Person() { 8 public void eat() { 9 System.out.println("eat something"); 10 } 11 }; 12 p.eat(); 13 } 14 }
運行結果:eat something
可以看到,我們直接將抽象類Person中的方法在大括號中實現了
這樣便可以省略一個類的書寫
並且,匿名內部類還能用於接口上
實例3:在接口上使用匿名內部類
1 interface Person { 2 public void eat(); 3 } 4 5 public class Demo { 6 public static void main(String[] args) { 7 Person p = new Person() { 8 public void eat() { 9 System.out.println("eat something"); 10 } 11 }; 12 p.eat(); 13 } 14 }
運行結果:eat something
由上面的例子可以看出,只要一個類是抽象的或是一個接口,那么其子類中的方法都可以使用匿名內部類來實現
最常用的情況就是在多線程的實現上,因為要實現多線程必須繼承Thread類或是繼承Runnable接口
實例4:Thread類的匿名內部類實現
1 public class Demo { 2 public static void main(String[] args) { 3 Thread t = new Thread() { 4 public void run() { 5 for (int i = 1; i <= 5; i++) { 6 System.out.print(i + " "); 7 } 8 } 9 }; 10 t.start(); 11 } 12 }
運行結果:1 2 3 4 5
實例5:Runnable接口的匿名內部類實現
1 public class Demo { 2 public static void main(String[] args) { 3 Runnable r = new Runnable() { 4 public void run() { 5 for (int i = 1; i <= 5; i++) { 6 System.out.print(i + " "); 7 } 8 } 9 }; 10 Thread t = new Thread(r); 11 t.start(); 12 } 13 }
運行結果:1 2 3 4 5
Lambda表達式
雖然使用 Lambda 表達式可以對某些接口進行簡單的實現,但並不是所有的接口都可以使用 Lambda 表達式來實現。Lambda 規定接口中只能有一個需要被實現的方法,不是規定接口中只能有一個方法
語法形式為 () -> {},其中 () 用來描述參數列表,{} 用來描述方法體,-> 為 lambda運算符 ,讀作(goes to)。
1 /**多參數無返回*/ 2 @FunctionalInterface 3 public interface NoReturnMultiParam { 4 void method(int a, int b); 5 } 6 7 /**無參無返回值*/ 8 @FunctionalInterface 9 public interface NoReturnNoParam { 10 void method(); 11 } 12 13 /**一個參數無返回*/ 14 @FunctionalInterface 15 public interface NoReturnOneParam { 16 void method(int a); 17 } 18 19 /**多個參數有返回值*/ 20 @FunctionalInterface 21 public interface ReturnMultiParam { 22 int method(int a, int b); 23 } 24 25 /*** 無參有返回*/ 26 @FunctionalInterface 27 public interface ReturnNoParam { 28 int method(); 29 } 30 31 /**一個參數有返回值*/ 32 @FunctionalInterface 33 public interface ReturnOneParam { 34 int method(int a); 35 }
1 import lambda.interfaces.*; 2 3 public class Test1 { 4 public static void main(String[] args) { 5 6 //無參無返回 7 NoReturnNoParam noReturnNoParam = () -> { 8 System.out.println("NoReturnNoParam"); 9 }; 10 noReturnNoParam.method(); 11 12 //一個參數無返回 13 NoReturnOneParam noReturnOneParam = (int a) -> { 14 System.out.println("NoReturnOneParam param:" + a); 15 }; 16 noReturnOneParam.method(6); 17 18 //多個參數無返回 19 NoReturnMultiParam noReturnMultiParam = (int a, int b) -> { 20 System.out.println("NoReturnMultiParam param:" + "{" + a +"," + + b +"}"); 21 }; 22 noReturnMultiParam.method(6, 8); 23 24 //無參有返回值 25 ReturnNoParam returnNoParam = () -> { 26 System.out.print("ReturnNoParam"); 27 return 1; 28 }; 29 30 int res = returnNoParam.method(); 31 System.out.println("return:" + res); 32 33 //一個參數有返回值 34 ReturnOneParam returnOneParam = (int a) -> { 35 System.out.println("ReturnOneParam param:" + a); 36 return 1; 37 }; 38 39 int res2 = returnOneParam.method(6); 40 System.out.println("return:" + res2); 41 42 //多個參數有返回值 43 ReturnMultiParam returnMultiParam = (int a, int b) -> { 44 System.out.println("ReturnMultiParam param:" + "{" + a + "," + b +"}"); 45 return 1; 46 }; 47 48 int res3 = returnMultiParam.method(6, 8); 49 System.out.println("return:" + res3); 50 } 51 }
lambda 表達式創建線程
我們以往都是通過創建 Thread 對象,然后通過匿名內部類重寫 run() 方法,一提到匿名內部類我們就應該想到可以使用 lambda 表達式來簡化線程的創建過程。
1 Thread t = new Thread(() -> { 2 for (int i = 0; i < 10; i++) { 3 System.out.println(2 + ":" + i); 4 } 5 }); 6 t.start();
1 Runnable r = -> { 2 for (int i=0;i<10;i++){ 3 System.out.println(2 + "." + i); 4 } 5 }; 6 Thread t = new Thread(r);
構造方法的引用
一般我們需要聲明接口,該接口作為對象的生成器,通過 類名::new 的方式來實例化對象,然后調用方法返回對象。
1 interface ItemCreatorBlankConstruct { 2 Item getItem(); 3 } 4 interface ItemCreatorParamContruct { 5 Item getItem(int id, String name, double price); 6 } 7 8 public class Exe2 { 9 public static void main(String[] args) { 10 ItemCreatorBlankConstruct creator = () -> new Item(); 11 Item item = creator.getItem(); 12 13 ItemCreatorBlankConstruct creator2 = Item::new; 14 Item item2 = creator2.getItem(); 15 16 ItemCreatorParamContruct creator3 = Item::new; 17 Item item3 = creator3.getItem(112, "鼠標", 135.99); 18 } 19 }
lambda 表達式引用方法
有時候我們不是必須要自己重寫某個匿名內部類的方法,我們可以可以利用 lambda表達式的接口快速指向一個已經被實現的方法。
語法
方法歸屬者::方法名 靜態方法的歸屬者為類名,普通方法歸屬者為對象
1 public class Exe1 { 2 public static void main(String[] args) { 3 ReturnOneParam lambda1 = a -> doubleNum(a); 4 System.out.println(lambda1.method(3)); 5 6 //lambda2 引用了已經實現的 doubleNum 方法 7 ReturnOneParam lambda2 = Exe1::doubleNum; 8 System.out.println(lambda2.method(3)); 9 10 Exe1 exe = new Exe1(); 11 12 //lambda4 引用了已經實現的 addTwo 方法 13 ReturnOneParam lambda4 = exe::addTwo; 14 System.out.println(lambda4.method(2)); 15 } 16 17 /** 18 * 要求 19 * 1.參數數量和類型要與接口中定義的一致 20 * 2.返回值類型要與接口中定義的一致 21 */ 22 public static int doubleNum(int a) { 23 return a * 2; 24 } 25 26 public int addTwo(int a) { 27 return a + 2; 28 } 29 }