欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

簡單了解Java的默認(rèn)和靜態(tài)方法

 更新時(shí)間:2020年01月03日 09:19:43   作者:天喬巴夏丶  
這篇文章主要介紹了簡單了解Java的默認(rèn)和靜態(tài)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

這篇文章主要介紹了簡單了解Java的默認(rèn)和靜態(tài)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

淺談Java的默認(rèn)和靜態(tài)方法

允許在接口中聲明默認(rèn)方法和靜態(tài)方法,是JDK1.8新增的特性。存在即合理,兩者的出現(xiàn),讓接口越來越像抽象類,那么它們?yōu)槭裁闯霈F(xiàn)呢,它們的出現(xiàn)產(chǎn)生了哪些便利,Java小白開始學(xué)習(xí)并總結(jié),不足之處,還望評論區(qū)指點(diǎn)一二!

Java新增默認(rèn)方法有啥用

官方解答:默認(rèn)方法允許您添加新的功能到現(xiàn)有庫的接口中,并能確保與采用舊版本接口編寫的代碼的二進(jìn)制兼容性。
這個光看枯燥的介紹好像很難理解,舉個簡單的例子。假設(shè)有一個很大很大的項(xiàng)目,一個接口被很多很多的類所實(shí)現(xiàn),大家都平安無事平穩(wěn)地運(yùn)行著。突然有一天,出現(xiàn)了一個小小地問題,或者說有一個更好的優(yōu)化方案,需要在這些實(shí)現(xiàn)類去增加。在默認(rèn)方法出現(xiàn)之前,只有抽象方法,且需要在實(shí)現(xiàn)類中給出具體定義才能操作,那豈不是只能兩眼一閉,直接從早干到晚地添加啦。

但是,默認(rèn)方法地出現(xiàn)允許在接口中給出方法的具體實(shí)現(xiàn),且實(shí)現(xiàn)類中能夠自動實(shí)現(xiàn)默認(rèn)方法,我只需要將這個優(yōu)化放在接口的默認(rèn)方法里面,就能完成對所有實(shí)現(xiàn)類的優(yōu)化啦。當(dāng)然,純屬個人理解,如果我的例子有不恰當(dāng)?shù)牡胤?,歡迎指正哦。

package com.my.pac21;

/**
 * @auther Summerday
 */

interface Closable {
  void close();
  //假設(shè)是新增的默認(rèn)方法
  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

    //實(shí)現(xiàn)默認(rèn)方法
    cw.makeSound();//peng!
    cd.makeSound();//need to override default methods
  }
}

Java新增的靜態(tài)方法有啥用

默認(rèn)方法和靜態(tài)方法的在接口的出現(xiàn)讓接口失去“全是抽象方法”的特性,在探究完新增的默認(rèn)方法之后,我們該對靜態(tài)方法下手啦。開始瘋狂查找資料。。。

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上找到的答案,什么意思呢,在沒有新增靜態(tài)方法之前,我們?nèi)绻胱屢恍┕潭ǖ牟僮髟诮涌谥谐霈F(xiàn),就必須定義一個和接口配套的實(shí)現(xiàn)類。而接口中靜態(tài)方法的出現(xiàn),可以直接通過接口調(diào)用靜態(tài)方法。

package com.my.pac21;

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

這是一個我自認(rèn)為還比較幼稚的例子,僅供理解。

普通抽象方法的情況:我在接口中定義了一個抽象方法,而后我又定義了實(shí)現(xiàn)該方法的實(shí)現(xiàn)類,最后通過創(chuàng)建實(shí)現(xiàn)類的實(shí)例來調(diào)用該方法,最后算得兩值之和。可以想象,在實(shí)際中,如果相同性質(zhì)的方法想要在多個實(shí)現(xiàn)類中實(shí)現(xiàn),這種做法是比較麻煩的。

靜態(tài)方法的情況:就很直接地在接口中定義靜態(tài)方法,且可以被接口直接調(diào)用,不需要再定義與其配套的實(shí)現(xiàn)類,多舒服哦。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論