java匿名类

Java anonymous class are like local class or inner class without a name. We can use java anonymous class to declare and instantiate a class at the same time.

Java匿名类就像没有名称的本地类或内部类。 我们可以使用Java匿名类同时声明和实例化一个类。

Java匿名类 (Java Anonymous Class)

Java anonymous class is a nested or local class. You should use them only when you want to use local class only once. Let’s have a look at an example of anonymous class in java program.

Java匿名类是嵌套或本地类。 仅当您只想使用本地类一次时,才应使用它们。 让我们看一下Java程序中的匿名类的示例。

package com.journaldev.java.examples;public interface Hello {public void sayHello();
}

Java匿名类示例 (Java Anonymous Class Example)

Hello is an interface, let’s see how we can create an anonymous class implementation of Hello interface.

Hello是一个接口,让我们看看如何创建Hello接口的匿名类实现。

package com.journaldev.java.examples;public class AnonymousExample {//nested anonymous classpublic static Hello hello = new Hello() {@Overridepublic void sayHello() {System.out.println("Hello nested anonymous class");}};public static void main(String[] args) {//anonymous class inside methodHello h = new Hello() {@Overridepublic void sayHello() {System.out.println("Hello anonymous class");}};h.sayHello();AnonymousExample.hello.sayHello();}}

Above Hello class can be an abstract class also, like below.

Hello类上方也可以是抽象类,如下所示。

package com.journaldev.java.examples;public abstract class Hello {abstract void sayHello();
}

Not only that, Hello can be a normal top level class also, like below.

不仅如此,Hello也可以是普通的顶级类,如下所示。

package com.journaldev.java.examples;public class Hello {public void sayHello(){};
}

Notice that anonymous classes are expressions and ends with semicolon.

请注意,匿名类是表达式,并以分号结尾。

Anonymous class is defined by calling class constructor followed by class definition code inside curly braces.

匿名类是通过在大括号内调用类构造函数和类定义代码来定义的。

Since anonymous class don’t have a name, we can’t define a constructor inside the class code body.

由于匿名类没有名称,因此我们无法在类代码主体内定义构造函数。

具有构造函数参数的Java匿名类示例 (Java Anonymous class example with constructor argument)

What if our Hello class don’t have no-args constructor? Can we access class variables in the anonymous class? Do we need to override all the methods of class in anonymous class?

如果我们的Hello类没有no-args构造函数怎么办? 我们可以访问匿名类中的类变量吗? 我们需要重写匿名类中的所有类方法吗?

Let’s answer above questions with a simple code example.

让我们用一个简单的代码示例来回答上述问题。

package com.journaldev.java.examples;public class Hello {protected String s;public Hello(String str){this.s = str;}public void sayHello(){System.out.println(s);};void foo(){};
}
package com.journaldev.java.examples;public class AnonymousExample {public static void main(String[] args) {//anonymous class inside methodHello h = new Hello("abc") {@Overridepublic void sayHello() {System.out.println("Hello anonymous class "+s);}};h.sayHello();}}

Java匿名类要点 (Java Anonymous Class Important Points)

  1. We can use any constructor while creating anonymous class. Notice the constructor argument is being used too.创建匿名类时,我们可以使用任何构造函数。 注意,构造函数参数也被使用。
  2. Anonymous class extends the top-level class and implements the abstract class or interface. So access modifier rules apply as usual. We are able to access protected variable, if we change it to private then we won’t be able to access it.匿名类扩展顶级类并实现抽象类或接口。 因此,访问修饰符规则照常适用。 我们能够访问受保护的变量,如果将其更改为私有变量,则将无法访问它。
  3. Since we are extending the Hello class above, we are not required to override all the methods. However if it would have been an interface or abstract class then we have to provide implementation of all the unimplemented methods.由于我们在上面扩展了Hello类,因此不需要覆盖所有方法。 但是,如果它将是接口或抽象类,那么我们必须提供所有未实现方法的实现。
  4. You cannot declare static initializers or member interfaces in an anonymous class.您不能在匿名类中声明静态初始化器或成员接口。
  5. An anonymous class can have static members provided that they are constant variables.匿名类可以具有静态成员,前提是它们是常量变量。

That’s all for anonymous class in java.

Java的匿名类就这些了。

翻译自: https://www.journaldev.com/12534/java-anonymous-class

java匿名类

java匿名类_Java匿名类相关推荐

  1. java 嵌套调用_Java嵌套类的使用

    嵌套类是指被定义在另一个类内部的类,它为外部类提供服务.嵌套类分四种:静态成员类.非静态成员类.匿名类和局部类. 一.静态成员类与非静态成员类的区别?在什么情况下可以用静态成员类? 我们知道在类的设计 ...

  2. java filereader类_Java FileReader类

    FileReader类从InputStreamReader类继承而来.该类按字符读取流中数据.可以通过以下几种构造方法创建需要的对象. 在给定从中读取数据的 File 的情况下创建一个新 FileRe ...

  3. java复用类_java复用类

    1. toString() 每一个非基本类型都有一个toString()方法:当编译器需要从对象获取一个string时,该对象的toString()方法就会被调用. 示例: class WaterSo ...

  4. java高级类_Java高级类特性(一)

    权限类内同包不同包子类不同包非子类 private √ × × × default √ √ × × protected √ √ √ × public √ √ √ √ 四.super关键字的使用 pac ...

  5. java 根据类名示例化类_Java即时类| from()方法与示例

    java 根据类名示例化类 即时类from()方法 (Instant Class from() method) from() method is available in java.time pack ...

  6. java 根据类名示例化类_Java即时类| EpochSecond()方法的示例

    java 根据类名示例化类 EpochSecond()方法的即时类 (Instant Class ofEpochSecond() method) Syntax: 句法: public static I ...

  7. java 大数类_Java大数类介绍

    java能处理大数的类有两个高精度大整数BigInteger和高精度浮点数BigDecimal,这两个类位于java.math包内,要使用它们必须在类前面引用该包:import java.math.B ...

  8. java 根据类名示例化类_Java即时类| plusMillis()方法与示例

    java 根据类名示例化类 即时类plusMillis()方法 (Instant Class plusMillis() method) plusMillis() method is available ...

  9. java 根据类名示例化类_Java LocalDateTime类| atOffset()方法与示例

    java 根据类名示例化类 LocalDateTime类atOffset()方法 (LocalDateTime Class atOffset() method) atOffset() method i ...

最新文章

  1. 纯Rust编写的机器学习框架Neuronika,速度堪比PyTorch
  2. iOS开发中接口调用使用https
  3. 使用 laravel 命令安装 Laravel
  4. java父类转换成子类_【转】java 父类与子类的转换
  5. Business Component(BC)和Business Object(BO)
  6. 2003 r2 64 iis php mysql_关于在win2003中,iis+php+mysql 配置的问题
  7. 每日小记2017.2.28
  8. android第三方launcher,目前Android平台最好的Launcher
  9. 大数据分析的关键特征有哪些
  10. json数据格式在javascript的读取与c#后台的赋值格式
  11. 3.2 如何判断Java对象的存活
  12. Nodejs与Java通用AES加解密
  13. vs2017安装勾选哪些_医学图像处理 VS2017配置ITK
  14. android 图标居中,文字和图标在部分安卓(小米、魅族)居中对其问题?
  15. graphpad如何加标注_GraphPad Prism绘图教程 | 如何在图表里插入特殊字符/符号
  16. 通往诺贝尔奖之路:盘点10个著名的科学家族
  17. linux中ifconfig命令作用,ifconfig命令作用范围的是什么
  18. 关于软件开发中遇到的问题解决思路
  19. CF888G - Xor-MST(顺带学习Borůvka算法)
  20. 微信接龙,查人,查谁没有接龙,工具

热门文章

  1. 一步步学习微软InfoPath2010和SP2010--第十二章节--管理和监控InfoPath Form Services(IPFS)(4)--监控含图片控件的Products表单...
  2. [原]批量生成AWR报告
  3. [转载] 利用python对csv文件进行简单的数据分析
  4. BZOJ3295 [Cqoi2011]动态逆序对 分治 树状数组
  5. Delphi XE5教程1:语言概述
  6. [Silverlight入门系列]动态创建控件和绑定
  7. 文件的I/O c++
  8. 数据结构笔记(二十二)--已知先序中序求树
  9. 【OpenCV】图像的内存分配与释放以及复制图像
  10. matlab 修正后阿尔法,修正后的阿尔法均值滤波器Alpha.ppt