1.什么是匿名内部类?
1.特征上:没有名字;
2.实现上:既可以扩展类,也可以实现接口,但是不能两者兼备;
3.限制上:如果实现接口,只能实现一个接口;
4.功能上:简化代码,符合优先使用类而不是接口的设计思想;
举例:
/**
* @description Service接口
**/
interface Service{
/**
* @description 接口方法1
* @Param []
* @return void
**/
void method1();
/**
* @description 接口方法2
* @Param []
* @return void
**/
void method2();
}
/**
* @description ServiceFactory接口
* @Param
* @return
**/
interface ServiceFactory{
/**
* @description 方法
* @Param []
* @return com.ehai.test.Service
**/
Service getSercice();
}
/**
* @description Service接口实现类1
* @Param
* @return
**/
class Implementation1 implements Service {
private Implementation1() { }
@Override
public void method1() {
System.out.println("Implementation1 method1");
}
@Override
public void method2() {
System.out.println("Implementation1 method2");
}
/**
* 利用匿名内部类创建静态ServiceFactory对象
**/
public static ServiceFactory factory1 = new ServiceFactory() {
@Override
public Service getSercice() {
return new Implementation1();
}
};
}
/**
* @description Service接口实现类2
* @Param
* @return
**/
class Implementation2 implements Service {
private Implementation2() { }
@Override
public void method1() {
System.out.println("Implementation2 method1");
}
@Override
public void method2() {
System.out.println("Implementation2 method2");
}
/**
* 利用匿名内部类创建静态ServiceFactory对象
* JDK8之前
**/
public static ServiceFactory factory1 = new ServiceFactory() {
@Override
public Service getSercice() {
return new Implementation2();
}
};
}
/**
* @description 公共类Factories
* @Param
* @return
**/
public class Factories {
public static void serviceConsumer(ServiceFactory fact){
Service s = fact.getSercice();
s.method1();
s.method2();
}
public static void main(String[] args){
serviceConsumer(Implementation1.factory);
serviceConsumer(Implementation2.factory);
}
}
2.什么是函数式接口?
1.特征上:只有一个抽象方法,可以包含多个默认方法,类方法;
2.限制上:在JDK8之后应用;
3.注解上:@FunctionalInterface;
引用上面匿名内部类例子中的代码:
/**
* @description ServiceFactory接口
* @Param
* @return
**/
interface ServiceFactory{
/**
* @description 方法
* @Param []
* @return com.ehai.test.Service
**/
Service getSercice();
}
3.Lambda表达式在匿名内部类中的应用
1.功能上:简化代码,提升开发效率;
2.限制上:JDK8以后;
3.形式上:应用于函数式接口上的表达式;
针对上面Implementation1 和Implementation2 类的例子利用函数式编程进行改进如下:
/**
* @description Service接口实现类1
* @Param
* @return
**/
class Implementation1 implements Service {
private Implementation1() { }
@Override
public void method1() {
System.out.println("Implementation1 method1");
}
@Override
public void method2() {
System.out.println("Implementation1 method2");
}
/**
* 利用lambda表达式创建静态ServiceFactory对象
* JDK8之后
**/
public static ServiceFactory factory = () -> new Implementation1();
}
/**
* @description Service接口实现类2
* @Param
* @return
**/
class Implementation2 implements Service {
private Implementation2() { }
@Override
public void method1() {
System.out.println("Implementation2 method1");
}
@Override
public void method2() {
System.out.println("Implementation2 method2");
}
/**
* 利用lambda表达式创建静态ServiceFactory对象
* JDK8之后
**/
public static ServiceFactory factory = () -> new Implementation2();
}
总结
Lambda表达式在一定程度上可以简化匿名内部类的代码,都可以直接访问final修饰的局部变量,外部类的成员变量,但是二者有实质性的区别;
1.匿名内部类通过new表达式返回的引用被自动向上转型为对ServiceFactory的引用;
2.匿名内部类可以应用于任何接口,Lambda表达式只可以应用于函数式接口,;
3.匿名内部类可以调用接口默认方法,Lambda表达式不可以;