匿名實現類&匿名對象


學習過程中經常搞不清匿名類&匿名對象怎么用,今天就把常用的方式總結一遍。

1.創建了非匿名實現類的非匿名對象
 1 //定義USB接口
 2 interface USB{
 3     void inputInofo();
 4     void outputInfo();
 5 }
 6 //USB接口的實現類
 7 class Computer implements USB{
 8 
 9     public void inputInofo() {
10         System.out.println("MyComputer輸入信息[^L^]。。。。。。");
11     }
12 
13     public void outputInfo() {
14         System.out.println("MyComputer輸出信息[^_^]。。。。。。");
15     }
16 }
17 
18 public class mainTest {
19     @Test
20     public void show(){
21         //1.創建了非匿名實現類的非匿名對象(有實現類名,有對象名)
22         Computer computer = new Computer(); // ==> USB usbImpl = new Computer();
23         streamData(computer);
24     }
25 
26     public void streamData(USB usbImpl){
27         usbImpl.inputInofo();
28         usbImpl.outputInfo();
29     }
30 }
2.創建了非匿名實現類的匿名對象
 1 //定義USB接口
 2 interface USB{
 3     void inputInofo();
 4     void outputInfo();
 5 }
 6 //USB接口的實現類
 7 class Computer implements USB{
 8 
 9     public void inputInofo() {
10         System.out.println("MyComputer輸入信息[^L^]。。。。。。");
11     }
12 
13     public void outputInfo() {
14         System.out.println("MyComputer輸出信息[^_^]。。。。。。");
15     }
16 }
17 
18 public class mainTest {
19     @Test
20     public void show(){
21         //2.創建了非匿名實現類的匿名對象(有實現類名,沒有對象名),通常作為參數,不用定義變量名了
22         streamData(new Computer());
23     }
24 
25     public void streamData(USB usbImpl){
26         usbImpl.inputInofo();
27         usbImpl.outputInfo();
28     }
29 }
3.創建了匿名實現類的非匿名對象
 1 //定義USB接口
 2 interface USB{
 3     void inputInofo();
 4     void outputInfo();
 5 }
 6 //USB接口的實現類
 7 class Computer implements USB{
 8 
 9     public void inputInofo() {
10         System.out.println("MyComputer輸入信息[^L^]。。。。。。");
11     }
12 
13     public void outputInfo() {
14         System.out.println("MyComputer輸出信息[^_^]。。。。。。");
15     }
16 }
17 
18 public class mainTest {
19     @Test
20     public void show(){
21         //3.創建了匿名實現類的非匿名對象(沒有實現類名,有對象名)
22         USB usbImpl = new USB() {
23             public void inputInofo() {
24                 System.out.println("[匿名實現類,非匿名對象]輸入。。。。");
25             }
26 
27             public void outputInfo() {
28                 System.out.println("[匿名實現類,非匿名對象]輸出。。。。");
29             }
30         };
31 
32         streamData(usbImpl);
33     }
34 
35     public void streamData(USB usbImpl){
36         usbImpl.inputInofo();
37         usbImpl.outputInfo();
38     }
39 }
4.創建了匿名實現類的匿名對象
 1 //定義USB接口
 2 interface USB{
 3     void inputInofo();
 4     void outputInfo();
 5 }
 6 //USB接口的實現類
 7 class Computer implements USB{
 8 
 9     public void inputInofo() {
10         System.out.println("MyComputer輸入信息[^L^]。。。。。。");
11     }
12 
13     public void outputInfo() {
14         System.out.println("MyComputer輸出信息[^_^]。。。。。。");
15     }
16 }
17 
18 public class mainTest {
19     @Test
20     public void show(){
21         //4.創建了匿名實現類的匿名對象(沒有實現類名,沒有對象名),通常作為參數,不用定義變量名了
22         streamData(
23                 new USB()
24                 {
25                     public void inputInofo() {
26                         System.out.println("[匿名實現類,匿名對象]輸入。。。。");
27                     }
28 
29                     public void outputInfo() {
30                         System.out.println("[匿名實現類,匿名對象]輸出。。。。");
31                     }
32                 }
33         );
34     }
35 
36     public void streamData(USB usbImpl){
37         usbImpl.inputInofo();
38         usbImpl.outputInfo();
39     }
40 }


免責聲明!

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



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