博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java匿名类_Java匿名类
阅读量:2533 次
发布时间:2019-05-11

本文共 3932 字,大约阅读时间需要 13 分钟。

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 class	public static Hello hello = new Hello() {		@Override		public void sayHello() {			System.out.println("Hello nested anonymous class");		}	};		public static void main(String[] args) {				//anonymous class inside method		Hello h = new Hello() {			@Override			public 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 method		Hello h = new Hello("abc") {			@Override			public 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的匿名类就这些了。

翻译自:

java匿名类

转载地址:http://zmlzd.baihongyu.com/

你可能感兴趣的文章
iOS开发网络篇—XML数据的解析
查看>>
[BZOJ4303]数列
查看>>
一般处理程序在VS2012中打开问题
查看>>
C语言中的++和--
查看>>
thinkphp3.2.3入口文件详解
查看>>
POJ 1141 Brackets Sequence
查看>>
Ubuntu 18.04 root 使用ssh密钥远程登陆
查看>>
Servlet和JSP的异同。
查看>>
虚拟机centOs Linux与Windows之间的文件传输
查看>>
ethereum(以太坊)(二)--合约中属性和行为的访问权限
查看>>
IOS内存管理
查看>>
middle
查看>>
[Bzoj1009][HNOI2008]GT考试(动态规划)
查看>>
Blob(二进制)、byte[]、long、date之间的类型转换
查看>>
OO第一次总结博客
查看>>
day7
查看>>
iphone移动端踩坑
查看>>
vs无法加载项目
查看>>
Beanutils基本用法
查看>>
玉伯的一道课后题题解(关于 IEEE 754 双精度浮点型精度损失)
查看>>