Consumer默認(rèn)實(shí)現(xiàn)中使用this出現(xiàn)的問題(未解決)

Consumer 接收一個(gè)泛型T,不返回值。
不知道為什么這樣使用會(huì)報(bào)錯(cuò)
new IThisImpl().forEach1((IThisImpl t) -> t.getone());
但是分開寫的話就可以,

Consumer<IThisImpl> action = (t) -> t.getone();
new IThisImpl().forEach1(action);

public interface IThis<T> {

    default void forEach1(Consumer<IThisImpl> action) {
        Objects.requireNonNull(action);
        //默認(rèn)實(shí)現(xiàn)  中使用 的this是實(shí)現(xiàn)的this對(duì)象  這里是IThisImpl對(duì)象
        action.accept((IThisImpl) this);
    }
}




    List list = new ArrayList();
    public IThisImpl(){
        list = Arrays.asList("tom","jack");
    }
    public int gettwo(){
        return 2;
    }
    public void getone(){
        System.out.println((String) list.get(0));
    }
    @Test
    public void test() {
        //
        Consumer<IThisImpl> action = (t) -> t.getone();
        new IThisImpl().forEach1(action);
        //(IThisImpl t) -> t.getone()
//        new IThisImpl().forEach1((IThisImpl t) -> t.getone());
//        new IThisImpl().getone();
    }
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 134,991評(píng)論 19 139
  • 前言 人生苦多,快來 Kotlin ,快速學(xué)習(xí)Kotlin! 什么是Kotlin? Kotlin 是種靜態(tài)類型編程...
    任半生囂狂閱讀 26,282評(píng)論 9 118
  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法,類相關(guān)的語法,內(nèi)部類的語法,繼承相關(guān)的語法,異常的語法,線程的語...
    子非魚_t_閱讀 31,767評(píng)論 18 399
  • 對(duì)象的創(chuàng)建與銷毀 Item 1: 使用static工廠方法,而不是構(gòu)造函數(shù)創(chuàng)建對(duì)象:僅僅是創(chuàng)建對(duì)象的方法,并非Fa...
    孫小磊閱讀 2,031評(píng)論 0 3
  • 剛剛拒絕了一個(gè)追求者,愛情并不是一個(gè)人的事情,不喜歡就不要去勉強(qiáng),傷害了誰都不好,勿忘心安
    嵩家小戀Vcn閱讀 193評(píng)論 0 0