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平臺

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