我正在 this tutorial 中阅读有关 Java 中的函数式接口(interface)的内容
这是困扰我的具体代码:
public interface FunctionalInterfaceTest{
void display();
}
//Test class to implement above interface
public class FunctionInterfaceTestImpl {
public static void main(String[] args){
//Old way using anonymous inner class
FunctionalInterfaceTest fit = new FunctionalInterfaceTest(){
public void display(){
System.out.println("Display from old way");
}};
fit.display();//outputs: Display from old way
//Using lambda expression
FunctionalInterfaceTest newWay = () -> {System.out.println("Display from new Lambda Expression");}
newWay.display();//outputs : Display from new Lambda Expression
}
}
我不明白。拥有一个名为 display()
的函数有什么意义。它不执行任何操作,也从未被定义。是的,我知道当您调用功能接口(interface)中的单个方法时,它会执行在其中创建的 lambda 表达式中的代码。
但这是我的问题;如果所有函数接口(interface)本质上只是运行 lambda 表达式,那么为什么不直接节省我们命名单个方法的时间,而将其永久设为 exe()
呢?如果几乎不添加任何内容,那么提供功能接口(interface)语法和定制还有什么意义呢?对我来说更好的语法是:
@FunctionalInterface MyInterface
MyInterface mine = () -> {System.out.print("Mine!!")}
mine.exe();
这更加标准、更短并且更容易理解。
这是一个绝妙的主意还是我错过了什么?
最佳答案
What is the point of having a function called display(). It doesn't do anything and is never defined.
它本质上仍然是一个接口(interface),并且如果您不使用 lambda,它仍然受到与接口(interface)相同的不稳定规则的约束。
可以以完全相同的方式在具体类上定义 display
方法...
public class Concrete implements FunctionalInterfaceTest {
@Override
public void display() {
System.out.println("Displayed! W00t!");
}
}
...我们会完成完全相同相同数量的工作:我们有一个只会产生副作用的接口(interface)。这是不可取的。
当您真正想做 pure 时,它的真正威力就会显现出来。对对象或基元的操作,但您不想仅仅为了指定这些操作实际是什么而定义具体的类。
假设我们想要一个函数式接口(interface)来提供两个 Number
的差异。
@FunctionalInterface
public interface NumberAdder<T extends Number> {
T result (T firstValue, T secondValue);
}
然后我可以这样使用它:
NumberAdder<Integer> numberAdder = (x, y) -> x + y;
NumberAdder<Long> longAdder = (x, y) -> x + y;
// From (x, y) -> x.add(y);, the below is shortened to a method reference
NumberAdder<BigInteger> bigIntAdder = BigInteger::add;
总的来说,它是子类化的语法糖(无论是否匿名),这必须在接口(interface)中发生。相反,我可以定义一次合约,并在我需要它的任何场景/上下文中定义行为。
System.out.println(numberAdder.result(10, 20));
System.out.println(longAdder.result(172893791273L, 979789123L));
System.out.println(bigIntAdder.result(BigInteger.valueOf(172917298179821L), BigInteger.valueOf(17232891L)));
定义行为仍然是您的责任。您可以轻松地这样写:
NumberAdder<BigInteger> bigIntAdder2 = BigInteger::divide;
...界面仍然很好。您需要小心,在实际实现时不要滥用您的预期用途。
不要将 lambda 视为其他任何东西。总的来说,您正在用 lambda 表达式替换匿名类。如果您想喜欢它,我们非常欢迎您,但不要假设每个功能接口(interface)之间都遵循相同的约定。
关于java - 在函数式接口(interface)中命名函数有什么意义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33603449/