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

Posted mirai3usi9

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[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

以上是关于[20-05-18][Thinking in Java 25]Java Inner Class 9 - Anonymous Inner Class 4的主要内容,如果未能解决你的问题,请参考以下文章

Thinking In Java

Hacker Rank: Two Strings - thinking in C# 15+ ways

Thinking in Java & Writing in Python

Thinking in LINQ, Chapter 1, Thinking Functionally

Thinking in Java

Thinking in React(翻译)