【发布时间】:2017-02-12 18:12:11
【问题描述】:
我的问题是 lambda 和方法引用都是关于函数式接口的。他们只是提供它们的实现。
现在当我写:
class Apple{
private int weight;
private String color;
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}}
如果我写:
Function<Apple, Integer> getWeight = Apple::getWeight;
或
appleList.stream().map(Apple::getColor).collect(toList());
它实际上是如何工作的,我的 getter 没有采用 Apple 的任何参数?因为根据功能功能接口
@FunctionalInterface
public interface Function<T, R> {
R apply(T t);}
它需要一个参数并返回一些东西,它应该确实有效 当吸气剂是这样的:
public int getWeight(Apple a) {
return a.weight;
}
我有点困惑提前谢谢
【问题讨论】:
标签: java collections java-8 method-reference