美文网首页
Consumer默认实现中使用this出现的问题(未解决)

Consumer默认实现中使用this出现的问题(未解决)

作者: KeDaiBiaO1 | 来源:发表于2017-11-28 10:53 被阅读0次

Consumer 接收一个泛型T,不返回值。
不知道为什么这样使用会报错
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);
        //默认实现  中使用 的this是实现的this对象  这里是IThisImpl对象
        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();
    }

相关文章

网友评论

      本文标题:Consumer默认实现中使用this出现的问题(未解决)

      本文链接:https://www.haomeiwen.com/subject/rqesbxtx.html