是否可以在使用者中运行一个方法,就像方法引用一样,但在传递给使用者的对象上运行:
Arrays.stream(log.getHandlers()).forEach(h -> h.close());
会是这样的:
Arrays.stream(log.getHandlers()).forEach(this::close);
但这不起作用......
是否有可能提供方法参考,或者是x -> x.method()
在这里工作的唯一方法是什么?
你不需要this
. YourClassName::close
将调用close
传递给消费者的对象上的方法:
Arrays.stream(log.getHandlers()).forEach(YourClassName::close);
有四种方法引用(Source http://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html):
Kind Example
---- -------
Reference to a static method ContainingClass::staticMethodName
Reference to an instance method of a particular object containingObject::instanceMethodName
Reference to an instance method of an arbitrary object of a particular type ContainingType::methodName
Reference to a constructor ClassName::new
就您而言,您需要第三种。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)