jdk8 FunctionalInterface注解源码解读
源码
package java.lang;
import java.lang.annotation.*;
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface FunctionalInterface {}Conceptually, a functional interface has exactly one abstract method.
Since default methods have an implementation, they are not abstract.
If an interface declares an abstract method overriding one of the public methods of java.lang.Object, that also does not count toward the interface's abstract method count since any implementation of the interface will have an implementation from java.lang.Object or elsewhere.
打捞要点之后有三点
- 要声明FunctionalInterface的接口只能有1个抽象方法;
a functional interface has exactly one abstract method.
- 因为default方法有实现,所以它也不是抽象方法, 所以不在列;
Since default methods have an implementation, they are not abstract.
- 因为这里限定抽象方法只能有1个,那么如果我的接口里声明了一个 抽象方法
"String toString();"或者"boolean equals(Object obj);"这样的,算不算呢? 答案是:不算这个,因为任何实现接口的对象都会继承Object,所以这些public的Object的方法,不算在列的。
If an interface declares an abstract method overriding one of the public methods of java.lang.Object, that also does not count toward the interface's abstract method count since any implementation of the interface will have an implementation from java.lang.Object or elsewhere.
接口声明
也就是说,你的接口可以声名如下:
/**
* Created by niewj on 2017/10/26.
*/
@FunctionalInterface
public interface FunctionalInterfaceSummary {
int doSum(int x, int y); // 抽象方法
String toString(); // 不占1的数额
boolean equals(Object obj); // 不占1的数额
// boolean equals(); // 占1的数额,这不是Object方法
default void display() {
System.out.println("show sth..");
}
}小结
1. 接口只限一个抽象方法,可注解@FunctionalInterface 限定;
2. 声明函数式接口之后,有以下特征:
a. 只有一个抽象方法;
b. Object类的public的方法也可以在接口里写出来,因为这不算;
c. default方法不算, 因为它们是implementation。不是abstract!
以上就是jdk8 FunctionalInterface注解源码解读的详细内容,更多关于jdk8 FunctionalInterface注解的资料请关注脚本之家其它相关文章!
相关文章
Spring Data JPA使用Sort进行排序(Using Sort)
本篇文章主要介绍了Spring Data JPA使用Sort进行排序(Using Sort),具有一定的参考价值,有兴趣的可以了解一下2017-07-07
spring security的BCryptPasswordEncoder加密和对密码验证的原理分析
文章介绍了加密算法和hash算法的基本概念,以及BCryptPasswordEncoder加密和解密的原理,加密算法是可逆的,需要加盐以保证安全性,BCryptPasswordEncoder通过生成盐值并在加密和解密过程中使用,确保相同的明文每次加密结果不同,从而提高安全性2024-11-11


最新评论