interface內的方法預設都是public abstract
但是現在可以利用關鍵字default來實踐方法內容
使用方法就是在該method前面加上關鍵字default
其他的部分就跟一般method一樣了
而使用時需要先實作implements該介面的class
然後就可以直接呼叫該method
另一種就是宣告static方法
用法和概念就跟在class內宣告static方法一樣
簡單的例子
public interface Iface {
static void showStatic () {
System.out.println("Static");
}
default void showMe(){
System.out.println("Me");
}
}
1. 官網https://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html
2. 參考網頁http://ebnbin.com/2015/12/20/java-8-default-methods/
文章標籤
全站熱搜
留言列表