免费观看又色又爽又黄的小说免费_美女福利视频国产片_亚洲欧美精品_美国一级大黄大色毛片

怎么對(duì)Java8中的函數(shù)式接口進(jìn)行測(cè)試-創(chuàng)新互聯(lián)

怎么對(duì)Java8中的函數(shù)式接口進(jìn)行測(cè)試?針對(duì)這個(gè)問題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡(jiǎn)單易行的方法。

創(chuàng)新互聯(lián)公司服務(wù)項(xiàng)目包括金秀網(wǎng)站建設(shè)、金秀網(wǎng)站制作、金秀網(wǎng)頁制作以及金秀網(wǎng)絡(luò)營(yíng)銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢(shì)、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,金秀網(wǎng)站推廣取得了明顯的社會(huì)效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到金秀省份的部分城市,未來相信會(huì)繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!

1. 函數(shù)式接口的理解

根據(jù)重構(gòu)的思想,需要把容易變化的模塊進(jìn)行抽象并封裝起來,從這個(gè)點(diǎn)來看,Java8新引入的函數(shù)式接口就是基于這個(gè)思想進(jìn)行設(shè)計(jì)的。

2. 函數(shù)式接口定義 

2.1 自定義如下

需要FunctionalInterface關(guān)鍵字顯示聲明:

@FunctionalInterface
 public interface AppleInterface {
public void test();
 }

2.2 系統(tǒng)預(yù)定義

java.util.function.Consumer;
java.util.function.Function;
java.util.function.Predicate;
java.util.function.Supplier;

可以去查看源碼了解具體的細(xì)節(jié),這幾個(gè)接口包括了常用的一些場(chǎng)景,一般可滿足需要

3. 函數(shù)式接口的使用

函數(shù)式接口一般使用前需要先定義,也可以使用系統(tǒng)預(yù)定義的幾個(gè)函數(shù)式接口

函數(shù)式接口的使用和使用一個(gè)變量沒有區(qū)別,顯示聲明定義,格式如下:

FunctionInterface interface=null;

這里的interface雖然看起來是一個(gè)變量,可是實(shí)際卻是一段行為代碼,用于執(zhí)行具體的業(yè)務(wù)邏輯,可以自由在方法接口間傳遞,也可以直接執(zhí)行

interface.doSomeThing();

如定義函數(shù)式接口為參數(shù)的接口:

public void filter(FunctionInterface interface)
{
 interface.doSomeThing();
}

4. 函數(shù)式接口練習(xí)

4.1 自定義實(shí)體類Apple

public class Apple {
  private String color;
  private float weight;

  public Apple(String color, float weight) {
  this.color = color;
  this.weight = weight;
  }

  public String getColor() {
  return color;
  }

  public void setColor(String color) {
  this.color = color;
  }

  public float getWeight() {
  return weight;
  }

  public void setWeight(float weight) {
  this.weight = weight;
  }
}

4.2 自定義函數(shù)式接口

該接口有一個(gè)test方法,不接收任何參數(shù),也沒有任何返回

@FunctionalInterface
public interface AppleInterface {
  public void test();
}

4.3 測(cè)試自定義函數(shù)式接口

 @Test
  public void DefineFunctionInterface(){
  //自定義函數(shù)式接口
  AppleInterface at=()->System.out.println("define FunctionInterface AppleInterface.");
  at.test();
  }

至此,就完成一個(gè)很簡(jiǎn)單的函數(shù)式接口的定義和調(diào)用

4.4 系統(tǒng)預(yù)定義函數(shù)式接口

Consumer<T>:該接口接收一個(gè)對(duì)象T,返回void,測(cè)試如下

 @Test
  public void ConsumerTest(){
  Consumer<Apple> consumer=(Apple app)->{System.out.println(app.getColor()+","+app.getWeight());};
  List<Apple> apps=Arrays.asList(new Apple("red", 120),new Apple("blue", 80),
 new Apple("green",100));
  ConsumerApple(apps,consumer);
  }

  public void ConsumerApple(List<Apple> apps,Consumer<Apple> c){
  for(Apple app:apps){
 c.accept(app);
  }
  }

Supplier<T>:該接口不接收任何參數(shù),返回一個(gè)對(duì)象T,測(cè)試如下:

 @Test
  public void SupplierTest(){
  Supplier<Apple> supplier=()->{return new Apple("hello supplier",999);};
  Apple app=supplier.get();
  System.out.println(app.getColor()+","+app.getWeight());
  }

Predicate<T>:該接口接收一個(gè)對(duì)象T,返回一個(gè)Boolean

 @Test
  public void PredicateTest(){
  //系統(tǒng)預(yù)定義函數(shù)式接口測(cè)試
  Predicate<Apple> p1=(Apple a)->{if(a.getWeight()>90) return true;return false;};
  Predicate<Apple> p2=(Apple a)->{if(a.getColor().equals("blue")) return true;return false;};

  List<Apple> apps=Arrays.asList(new Apple("red", 120),new Apple("blue", 80),
 new Apple("green",100));

  filterApple(apps,p1);//篩選重量大于90g的蘋果
  filterApple(apps,p2);//篩選藍(lán)色的蘋果
  }

  public void filterApple(List<Apple> apps,Predicate<Apple> p){
  for(Apple app:apps){
 if(p.test(app)){
 System.out.println(app.getColor()+","+app.getWeight());
 }
  }

  }

Function<T,R>: 該接口接收一個(gè)對(duì)象T,經(jīng)過轉(zhuǎn)換判斷,返回一個(gè)對(duì)象R

 @Test
  public void FunctionTest(){
  Function<String,Apple> function=(String s)->{return new Apple(s,666);};
  Apple app=function.apply("red");
  System.out.println(app.getColor()+","+app.getWeight());
  app=function.apply("green");
  System.out.println(app.getColor()+","+app.getWeight());

  }

關(guān)于怎么對(duì)Java8中的函數(shù)式接口進(jìn)行測(cè)試問題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識(shí)。

文章題目:怎么對(duì)Java8中的函數(shù)式接口進(jìn)行測(cè)試-創(chuàng)新互聯(lián)
文章位置:http://m.newbst.com/article26/dcspcg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供服務(wù)器托管、品牌網(wǎng)站設(shè)計(jì)、手機(jī)網(wǎng)站建設(shè)、小程序開發(fā)網(wǎng)站制作移動(dòng)網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

微信小程序開發(fā)