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 }
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 }
reference from: https://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html
原文地址:https://www.cnblogs.com/yangwu-183/p/13460327.html
- 使用R语言构造投资组合
- R语言构建追涨杀跌量化交易模型
- 美团点评境外度假团队前端项目开发实践总结
- 在jfinal中使用druid,并配置查看权限
- java中遇到过的String的一些特性
- Kaggle案例——使用scikit-learn解决DigitRecognition问题
- 基于Kaggle数据的词袋模型文本分类教程
- javascript中遇到的字符串对象处理
- 传统企业站开发 - 页面布局
- AngularJS中使用service,并同步数据
- 2016.05 第四周 群问题分享
- angularjs中 *.min.js.map 404的问题
- Git与通过Git添加提交文件
- Facebook社交网络R语言分析
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 2019-12-05-eck-qustion
- 聊聊dubbo-go的ConsumerSignFilter
- Javascript错误处理
- 程序员说模型过拟合的时候,说的是什么?
- 分分钟教会你搭建企业级的 npm 私有仓库
- Golang语言之字符串操作
- Go语言ORM-gorm学习笔记(二)
- TypeScript: 常用的高级类型
- TypeScript:得泛型者,得天下
- 微信PC端多开的秘密
- Spring Cloud 微服务(七)- 上:日志收集集成
- Spring Cloud 微服务(七)- 下:日志收集详解
- Spring Cloud 微服务(八):链路追踪
- 一文带你网络安全 入门到入* [网络安全]
- 小白也能秒懂Vue源码中那些精细设计(选项处理)