JDK的 “骚操作“ 之 Runnable

大家在平时工作学习中不知道用线程用的多不多,反正在面试里面你随时都在用且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平台

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章