第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

為什么當(dāng)我在 main 方法中實(shí)現(xiàn) lambda 表達(dá)式時(shí),編譯器不說(shuō)接口已實(shí)現(xiàn)?

為什么當(dāng)我在 main 方法中實(shí)現(xiàn) lambda 表達(dá)式時(shí),編譯器不說(shuō)接口已實(shí)現(xiàn)?

慕森王 2023-08-23 11:37:42
當(dāng)我在主方法中將接口實(shí)現(xiàn)為 Lambda 表達(dá)式時(shí),它不會(huì)被視為已實(shí)現(xiàn)。我知道我可以在 main 方法之外實(shí)現(xiàn)它,但是如果我必須在 main 方法之外實(shí)現(xiàn)它,我不明白為什么我應(yīng)該使用 Lambda 表達(dá)式。public class Driver implements Interface1, Interface2, Interface3 {    public static void main(String[] args) {        //Implementing Interface1        double x;        Interface1 obj = () -> 5.5;        x = obj.foo();        System.out.println(x);        //Implementing Interface2        String str;        Interface2 obj2 = (a) -> String.format("The number is %d", a);        str = obj2.foo(356);        System.out.println(str);        //Implementing Interface3        boolean tF;        Interface3 obj3 = (i, s) -> i == Integer.parseInt(s);        tF = obj3.foo(30, "30");        System.out.print(tF);    }在這里,我在第 1 行收到一條錯(cuò)誤消息,告訴我接口未實(shí)現(xiàn)。它仍然可以編譯并工作,我只是不明白為什么會(huì)收到此消息。目前的輸出是:5.5The number is 356true
查看完整描述

1 回答

?
守著一只汪

TA貢獻(xiàn)1872條經(jīng)驗(yàn) 獲得超4個(gè)贊

您所做的就是在 main 方法中定義局部變量,其類型恰好與類必須實(shí)現(xiàn)的接口一致。


您必須在類中定義為該類的所有接口提供實(shí)現(xiàn)的方法。例如:


public class Driver implements Interface1, Interface2, Interface3 {

    public static void main(String[] args) {

        // all code in here is irrelevant to the class implementing Interface1, Interface2, Interface3

    }


    public void interface1Method() {

        // whatever

    }


    public void interface2Method() {

        // whatever

    }


    public void interface3Method() {

        // whatever

    }

}

請(qǐng)注意,您不能為此使用 lambda;Driver必須實(shí)際聲明其聲明正在實(shí)現(xiàn)的所有接口中缺少的方法的實(shí)現(xiàn)。


查看完整回答
反對(duì) 回復(fù) 2023-08-23
  • 1 回答
  • 0 關(guān)注
  • 175 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)