淺談Java的默認和靜態方法

淺談Java的默認和靜態方法

容許在接口中聲明默認方法和靜態方法,是JDK1.8新增的特性。存在即合理,二者的出現,讓接口愈來愈像抽象類(關於二者之別下一篇作總結),那麼它們爲何出現呢,它們的出現產生了哪些便利,Java小白開始學習並總結,不足之處,還望評論區指點一二!app

Java新增默認方法有啥用

官方解答默認方法容許您添加新的功能到現有庫的接口中,並能確保與採用舊版本接口編寫的代碼的二進制兼容性。
這個光看枯燥的介紹好像很難理解,舉個簡單的例子。假設有一個很大很大的項目,一個接口被不少不少的類所實現,你們都平安無事平穩地運行着。忽然有一天,出現了一個小小地問題,或者說有一個更好的優化方案,須要在這些實現類去增長。在默認方法出現以前,只有抽象方法,且須要在實現類中給出具體定義才能操做,那豈不是隻能兩眼一閉,直接從早幹到晚地添加啦。
可是,默認方法地出現容許在接口中給出方法的具體實現,且實現類中可以自動實現默認方法,我只須要將這個優化放在接口的默認方法裏面,就能完成對全部實現類的優化啦。固然,純屬我的理解,若是個人例子有不恰當的地方,歡迎指正哦。ide

package com.my.pac21;

/**
 * @auther Summerday
 */

interface Closable {
    void close();
    //假設是新增的默認方法
    default void makeSound() {
        System.out.println("peng!");
    }
}

interface Openable {
    default void makeSound() {
        System.out.println("peng!");
    }
}

class Window implements Closable {

    @Override
    public void close() {
        System.out.println("Window.close");
    }
}

public class Door implements Closable, Openable {

    @Override
    public void close() {
        System.out.println("Door.close");
    }

    //兩個接口中包含同名的方法,須要重寫,指定一個
    @Override
    public void makeSound() {
        System.out.println("need to override default methods");
    }

    public static void main(String[] args) {
        Closable cw = new Window();
        Closable cd = new Door();
        cw.close();//Window.close
        cd.close();//Door.close

        //實現默認方法
        cw.makeSound();//peng!
        cd.makeSound();//need to override default methods
    }
}

Java新增的靜態方法有啥用

默認方法和靜態方法的在接口的出現讓接口失去「全是抽象方法」的特性,在探究完新增的默認方法以後,咱們該對靜態方法下手啦。開始瘋狂查找資料。。。學習

Before Java 8 made it possible to declare static methods in interfaces, it was common practice to place these methods in companion utility classes. For example, the java.util.Collections class is a companion to the java.util.Collection interface, and declares static methods that would be more appropriate in the relevant Java Collections Framework interfaces. You no longer need to provide your own companion utility classes. Instead, you can place static methods in the appropriate interfaces, which is a good habit to cultivate.優化

這個是我在stack overflow上找到的答案,什麼意思呢,在沒有新增靜態方法以前,咱們若是想讓一些固定的操做在接口中出現,就必須定義一個和接口配套的實現類。而接口中靜態方法的出現,能夠直接經過接口調用靜態方法。code

package com.my.pac21;

/**
 * @auther Summerday
 */
public class Test {
    public static void main(String[] args) {
        int val1 = 5;
        int val2 = 6;
        //經過建立實現類的對象
        Countable b = new CountableCompanion();
        System.out.println(b.getNum(val1, val2));
        //直接經過接口調用
        Countable.getMul(val1,val2);
    }
}
interface Countable{
    //普通抽象方法
    int getNum(int a,int b);
    //靜態方法
    static int getMul(int a,int b){
        return a*b;
    }
}
//實現接口的實現類
class CountableCompanion implements Countable{
    @Override
    public int getNum(int a,int b) {
        return a+b;
    }
}

這是一個我自認爲還比較幼稚的例子,僅供理解。對象

  • 普通抽象方法的狀況:我在接口中定義了一個抽象方法,然後我又定義了實現該方法的實現類,最後經過建立實現類的實例來調用該方法,最後算得兩值之和。能夠想象,在實際中,若是相同性質的方法想要在多個實現類中實現,這種作法是比較麻煩的。
  • 靜態方法的狀況:就很直接地在接口中定義靜態方法,且能夠被接口直接調用,不須要再定義與其配套的實現類,多舒服哦。
相關文章
相關標籤/搜索