Anonymous Classes

时间:2020-08-08
本文章向大家介绍Anonymous Classes,主要包括Anonymous Classes使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

Notes

  They make code more consise

  We declare and instantiate them at the same time

  They like local classes but don't have a name

  We use them when we need them only once

Declaring Anonymous Classes

  They are expressions, so we can define the class in another expression, for example.

 1 public class AnonymousClasses {
 2 
 3   interface HelloWorld{
 4     void greet();
 5     void greetSomeone(String someone);
 6   }
 7 
 8   public void sayHello(){
 9 
10     // declaration of an local class
11     class EnglishGreeting implements HelloWorld{
12       String name = "world";
13       @Override
14       public void greet() {
15         greetSomeone("world");
16       }
17 
18       @Override
19       public void greetSomeone(String someone) {
20         name = someone;
21         System.out.println("Hello " + name);
22       }
23     }
24 
25     HelloWorld englishGreeting = new EnglishGreeting();
26 
27     // declare and instantiate an anonymous class at the same time
28     // in the initialization statements of the local variable
29     HelloWorld frenchGreeting = new HelloWorld() {
30       String name = "tout le monde";
31       @Override
32       public void greet() {
33         greetSomeone("tout le monde");
34       }
35 
36       @Override
37       public void greetSomeone(String someone) {
38         name = someone;
39         System.out.println("Salut " + someone);
40       }
41     }; // do not forget the ;
42 
43     englishGreeting.greet(); // Hello world
44     frenchGreeting.greetSomeone("Fred"); // Salut Fred
45   }
46 
47   public static void main(String[] args) {
48     new AnonymousClasses().sayHello();
49   }
50 }
View Code

   

Syntax of Anonymous Classes

  The new operator

  The name of an interface to implement or a class to extend

  Parentheses that contain the arguments to a constructor. Note: interface has no constructor, so use an empty parentheses

  A body, a class declaration body

  A semicolon. An anonymous class definition is an expression, which is a part of a statement, so must have a semicolon.

  

Accessing Local Variables of the Enclosing Scope, and Declaring and Accesssing Members of the Anonymous Class

  Rules are presented in the following code

 1 public class AnonymousClasses {
 2 
 3   interface HelloWorld{
 4     void myTest();
 5   }
 6 
 7   public String author = "yang";
 8   public static final Integer age = 23;
 9 
10   public void test(){
11     System.out.println("method member of enclosing class");
12   };
13 
14   public void sayHello(){
15 
16     String time = "2020/8/8";
17     String sex = "female";
18 
19     // declare and instantiate an anonymous class at the same time
20     // in the initialization statements of the local variable
21     HelloWorld frenchGreeting = new HelloWorld() {
22 
23       // can access members of its enclosing class
24       String myAuthor = author; // ok
25       Integer myAge = age; // ok
26 
27       // can access local variables in enclosing scope
28       // that are declared final or effectively final
29       String myTime = time; // ok
30 
31       // shadow the sex variable defined in enclosing scope
32       String sex = "male";
33 
34       // has the same restrictions as local classes to their members
35 
36       // can not declare static methods
37 //      public static void init(){}
38 
39       // can not declare member interfaces
40 //      interface person{}
41 
42       // can have static constant variables
43       static final String school = "USTC";
44 
45       // Note that you can declare the following
46       /**
47        * Fields
48        * Extra methods
49        * Instance initializers
50        * Local class
51        */
52       private Integer money = 100;
53       private String city;
54       public void extraMethod(){}
55       public void instanceInitializers(String city){
56         this.city = city;
57       }
58       class myInnerClass{}
59       
60       // can not declare constructors
61 
62       @Override
63       public void myTest(){
64         test(); // ok
65       }
66 
67     }; // do not forget the ;
68 
69     frenchGreeting.myTest();
70   }
71 }
View Code

reference from: https://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html

  

原文地址:https://www.cnblogs.com/yangwu-183/p/13460327.html