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();
}