Functional Programming
@FunctionalInterface interface Lf{ void dispaly(); }
@FunctionalInterface display programming interface defining a function, the function does not comply with the formula given programming interface
anonymous inner classes use
public class FunPrograming { public static void main(String[] args) { func func = new func() { @Override public void dispaly() { // TODO Auto-generated method stub System.out.println("dispaly()运行"); } }; func.dispaly(); } } dispaly()运行
lamada: copy parentheses, write death ->, landing braces {}
public class FunPrograming { public static void main(String[] args) { func func = ()->{ System.out.println("dispaly()运行"); }; func.dispaly(); } }
dispaly () operation
Interface with return value
@FunctionalInterface interface FUNC { // Invalid '@FunctionalInterface' Annotation; FUNC IS A Not Functional interface defines two methods will be given general, do not meet the functional programming public void dispaly (); public int the Add ( int X, int Y); }
// copy parentheses, hardcoded ->, floor braces} {
@FunctionalInterface interface FUNC { // public void dispaly (); public int the Add ( int X, int Y) ; } public class FunPrograming { public static void main ( String [] args) { FUNC FUNC = ( int X, int Y) -> { the System. OUT .println ( " the Add (int X, Y int) operation " ); return X + Y; }; System.out.println(func.add(3,5)); } } add(int x,int y)运行 8
Functional programming interfaces, how to define a plurality of methods
@FunctionalInterface interface func { default void dispaly() { System.out.println("default void dispaly()"); } default int mul(int x ,int y) { System.out.println("default int mul"); return x* y; } static int dec(int x,int y) { System.out.println("static int dec(int x,int y)"); return x-y; } static int div(int x,int y) { System.out.println("static int div(int x,int y)"); return x/y; } public int add(int x,int y); }