日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > java >内容正文

java

java请求接口示例_Java 8:功能接口示例

發(fā)布時(shí)間:2023/12/3 java 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java请求接口示例_Java 8:功能接口示例 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

java請(qǐng)求接口示例

為了支持Java 8中的lambda表達(dá)式,他們引入了Functional Interfaces。

具有單一抽象方法的接口可以稱為功能接口。

Runnable,Comparator,Cloneable是功能接口的一些示例。 我們可以使用Lambda表達(dá)式實(shí)現(xiàn)這些功能接口。

例如:

Thread t =new Thread(new Runnable(){public void run(){System.out.println("Runnable implemented by using Lambda Expression");} });

這是創(chuàng)建線程的舊方法。

由于Runnable具有單一抽象方法,我們可以將其視為功能接口,并且可以使用如下所示的Lambda表達(dá)式。

Thread t = new Thread(()->{System.out.println("Runnable implemented by using Lambda Expression"); });

在這里,我們沒(méi)有傳遞Runnable對(duì)象,而是傳遞了lambda表達(dá)式。

聲明我們自己的功能接口:

我們可以通過(guò)限定了S個(gè)英格爾一個(gè) bstract M在接口ethod宣布我們自己的功能界面。

public interface FunctionalInterfaceTest{ void display(); } //Test class to implement above interface public class FunctionInterfaceTestImpl {public static void main(String[] args){//Old way using anonymous inner classFunctionalInterfaceTest fit = new FunctionalInterfaceTest(){public void display(){System.out.println("Display from old way");}};fit.display();//outputs: Display from old way//Using lambda expressionFunctionalInterfaceTest newWay = () -> {System.out.println("Display from new Lambda Expression");}newWay.display();//outputs : Display from new Lambda Expression} }

我們可以使用@FunctionalInterface批注進(jìn)行批注,以告知編譯時(shí)錯(cuò)誤。 它是可選的

例如:

@FunctionalInterface public interface FunctionalInterfaceTest{void display();void anotherDisplay();//shows an error, FunctionalInterface should have only one abstarct method. }

默認(rèn)方法:

功能接口不能具有多個(gè)抽象方法,但是可以具有多個(gè)默認(rèn)方法。

Java 8中引入了默認(rèn)方法,以添加新方法進(jìn)行接口,而不會(huì)干擾已實(shí)現(xiàn)的類。

interface DefaultInterfaceTest{void show();default void display(){System.out.println("Default method from interface can have body..!");} } public class DefaultInterfaceTestImpl implements DefaultInterfaceTest{public void show(){System.out.println("show method");}//we dont need to provide any implementation to default method.public static void main(String[] args){DefaultInterfaceTest obj = new DefaultInterfaceTestImpl();obj.show();//out puts: show methodobj.display();//outputs : Default method from interface can have body..!} }

默認(rèn)方法的主要用途是不強(qiáng)制實(shí)現(xiàn)類,我們可以向接口添加方法。

多重繼承:

如果兩個(gè)接口中存在相同的默認(rèn)方法,并且一個(gè)類正在實(shí)現(xiàn)該接口,則它將引發(fā)錯(cuò)誤。

//Normal interface with show methodinterface Test{default void show(){System.out.println("show from Test");}}//Another interface with same show methodinterface AnotherTest{default void show(){System.out.println("show from Test");}}//Main class to implement above two interfacesclass Main implements Test, AnotherTest{ //here is an ambiguity which show method has to inherit here }

此類不會(huì)編譯,因?yàn)門est和AnotherTest接口show()方法之間存在歧義,要解決此問(wèn)題,我們需要將Show()方法覆蓋到Main Class。

class Main implements Test, AnotherTest{void show(){System.out.println("Main show method");}}

翻譯自: https://www.javacodegeeks.com/2014/05/java-8-functional-interface-example.html

java請(qǐng)求接口示例

總結(jié)

以上是生活随笔為你收集整理的java请求接口示例_Java 8:功能接口示例的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。