[20-05-18][Thinking in Java 25]Java Inner Class 9 - Anonymous Inner Class 4

 1 package test_16_2;
 2 
 3 public class Outter {
 4     
 5     public Outter() {
 6         
 7     }
 8     
 9     public Outter(int i) {
10         
11         this.i = i;
12     }
13 
14     private int i = 1;
15     
16     private void show() {
17         System.out.println("this is " + i);
18     }
19     
20     public Outter outter(int x) {
21         
22         return new Outter(x) {
23             
24             {
25                 i = x;
26                 show();
27             }
28         };
29     }
30     
31     public int getI() {
32         
33         return i;
34     }
35     
36     public static void main(String[] args) {
37         
38         Outter outter = new Outter();
39         outter.outter(10);
40         System.out.println(outter.getI());
41     }
42 }

结果如下:

this is 10
10

猜你喜欢

转载自www.cnblogs.com/mirai3usi9/p/12913841.html