JDK的 “骚操作“ 之 Runnable

2021/02/07 09:02
阅读数 55

大家在平时工作学习中不知道用线程用的多不多,反正在面试里面你随时都在用且6的飞起

不知道大家有没有注意到这样一个问题

@FunctionalInterface
public interface Runnable {
   
   
    public abstract void run();
}

如果你没发现有什么不妥

我就再贴一段代码

@FunctionalInterface
public interface Callable<V> {
   
   
    V call() throws Exception;
}

发没发现,Runnable里面接口定义方法冗余了 public abstract ,我有点纳闷

于是我找找找,找到这么一段话,来自于java language specification

Every method declaration in the body of an interface is implicitly abstract, so its body is always represented by a semicolon, not a block.

Every method declaration in the body of an interface is implicitly public.

For compatibility with older versions of the Java platform, it is permitted but discouraged, as a matter of style, to redundantly specify the abstract modifier for methods declared in interfaces.

It is permitted, but strongly discouraged as a matter of style, to redundantly specify the public modifier for interface methods.

说白了就是接口中的方法默认是 public abstract 的,java允许你添加这两个修饰符,但不鼓励哈,至于他自己为什么要这么做,maybe是为了兼容旧版本的Java平台

展开阅读全文
加载中

作者的其它热门文章

打赏
0
0 收藏
分享
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部