仅具有一种抽象方法的接口被称为功能接口,并且也被称为单一抽象方法接口(SAM接口)。一个抽象方法意味着允许使用默认方法或默认实现的抽象方法。SAM接口的实例是java.lang.Runnable,java.awt.event.ActionListen er,java.util.Comparator和java.util.concurrent.Callable。 SAM接口可以使用来实现的λ表达式或方法的引用。
语法@FunctionalInterface
public interface Changeable {
public void change(T o);
}
示例@FunctionalInterfaceinterface MyInterface {
String reverse(String n);
}
public class LambdaReverseTest {
public static void main( String[] args ) {
MyInterface myInterface = (str) -> { // Lambda Expression String result = "";
for(int i = str.length()-1; i >= 0 ; i--)
result += str.charAt(i);
return result;
};
System.out.println("The reverse of string is: " + myInterface.reverse("TutorialsPoint"));
}
}
输出结果The reverse of string is: tnioPslairotuT