[Java] function interface

Functional Interface

@FunctionalInterface
to restrict the interface is an interface function

@FunctionalInterface
public interface InterA {
	public abstract void print();
}

Lambda expressions omitted rules:
if only one process parameter, the data type and multi parentheses may be omitted
if there are a plurality of process parameters, the data type may be omitted
if only a single line of code in the braces, the braces, return, semicolons can be omitted (three must be omitted altogether)
-------------------------------------- --------
by Lambda calling method method expression. Output:

No call a method of the type parameter value no return.

main {
	method(()->{syso();});
	//省略格式
	method(()->syso());
}
public static void method(InterA i) {
	i.print();
}

A method call parameters return value type.

main {
	method(10, 20, (int num1, int num2)->{return num1 + num2;});
	//省略格式
	method(10, 20, (int num1, int num2)->num1 + num2);
}
public static void method(int num1, int num2, InterB i) {
	int result = i.getSum(num1, num2);
	syso(result);
}

Interface InterA:

@FunctionalInterface
public interface InterA {
	public abstract void print();
}

Interface InterB:

@FunctionalInterface
public interface InterB {
	public abstract int getSum(int num1, int num2);
}
Published 38 original articles · won praise 4 · Views 811

Guess you like

Origin blog.csdn.net/Hide111/article/details/105168322