匿名内部类:
它是一个没有名称的内部类,只为其创建一个对象。匿名内部类在使用某些“额外”(例如重载类或接口的方法)创建对象的实例时非常有用,而无需实际对类进行子类化。
匿名内部类在为图形编程中的侦听器接口编写实现类时很有用。
匿名内部类主要有两种创建方式:
- 类(可以是抽象的或具体的)
- 界面
语法:匿名类表达式的语法类似于构造函数的调用,除了在代码块中包含一个类定义。
// Test can be interface, abstract/concrete class
Test t = new Test()
{
// data members and methods
public void test_method()
{
........
........
}
};
为了理解匿名内部类,我们来看一个简单的程序
// Java program to demonstrate
// the need for Anonymous Inner class
interface Age {
int x = 21;
void getAge();
}
class AnonymousDemo {
public static void main(String[] args)
{
// Myclass is implementation class of Age interface
MyClass obj = new MyClass();
// calling getage() method implemented at Myclass
obj.getAge();
}
}
// Myclass implement the methods of Age Interface
class MyClass implements Age {
@Override
public void getAge()
{
// printing the age
System.out.print("Age is " + x);
}
}
输出:
Age is 21
拉姆达表达式:
Lambda 表达式基本上表达了函数式接口的实例(具有单一抽象方法的接口称为函数式接口。一个例子是Java.lang.Runnable)。 lambda 表达式实现了唯一的抽象函数,因此实现了函数式接口
lambda 表达式是在Java 8 中添加的,并提供以下功能。
- 启用将功能视为方法参数,或将代码视为数据。
- 可以在不属于任何类的情况下创建的函数。
- 一个 lambda 表达式可以像一个对象一样被传递并按需执行。
// Java program to demonstrate lambda expressions
// to implement a user defined functional interface.
// A sample functional interface (An interface with
// single abstract method
interface FuncInterface {
// An abstract function
void abstractFun(int x);
// A non-abstract (or default) function
default void
normalFun()
{
System.out.println("Hello");
}
}
class Test {
public static void main(String args[])
{
// lambda expression to implement above
// functional interface. This interface
// by default implements abstractFun()
FuncInterface fobj = (int x) -> System.out.println(2 * x);
// This calls above lambda expression and prints 10.
fobj.abstractFun(5);
}
}
输出:
10
差异表:
Anonymous Inner Class | Lambda Expression |
---|---|
It is a class without name. | It is a method without name.(anonymous function) |
It can extend abstract and concrete class. | It can’t extend abstract and concrete class. |
It can implement an interface that contains any number of abstract methods. | It can implement an interface which contains a single abstract methods. |
Inside this we can declare instance variables. | It does not allow declaration of instance variables, whether the variables declared simply act as local variables. |
Anonymous inner class can be instantiated. | Lambda Expression can’t be instantiated. |
Inside Anonymous inner class, “this” always refers to current anonymous inner class object but not to outer object. | Inside Lambda Expression, “this” always refers to current outer class object that is, enclosing class object. |
It is the best choice if we want to handle multiple methods. | It is the best choice if we want to handle interface. |
At the time of compilation, a separate .class file will be generated. | At the time of compilation, no separate .class file will be generated. It simply convert it into private method outer class. |
Memory allocation is on demand, whenever we are creating an onject. | It resides in a permanent memory of JVM. |