Created
June 21, 2013 17:04
-
-
Save chocotan/5832692 to your computer and use it in GitHub Desktop.
java8 test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface Father{ | |
default void eat() { | |
System.out.println("I am eating !"); | |
} | |
default void sleep() { | |
System.out.println("I am sleeping !"); | |
} | |
} | |
class Son implements Father{ | |
public void sleep(){ | |
System.out.println("Son is sleeping !"); | |
} | |
} | |
public class DefultMethodTest { | |
public static void main(String[] args){ | |
Father son = new Son(); | |
son.eat(); | |
son.sleep(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface A { | |
int cal(int a, int b); | |
} | |
public class LambdaTest { | |
public static int fun(int m, int n, A a) { | |
return a.cal(m, n); | |
} | |
public static void main(String[] args){ | |
System.out.println(fun(3, 2, new A(){ | |
public int cal(int a,int b){ | |
return a + b; | |
} | |
})); | |
//下面一行和上面一行等价 | |
System.out.print(fun(3, 2, (a,b) -> a + b)); | |
System.out.print(fun(3, 2, (a,b) -> a - b)); | |
System.out.print(fun(3, 2, (a,b) -> a * b)); | |
System.out.print(fun(3, 2, (a,b) -> a / b)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Calculator { | |
public static int add(int a,int b){ | |
return a+b; | |
} | |
} | |
class Student { | |
int id; | |
String name; | |
Student(int id,String name){ | |
this.id=id; | |
this.name=name; | |
} | |
} | |
interface StudentFactory{ | |
Student getStudent(int id,String name); | |
} | |
public class MethodRefTest { | |
public static void main(String[] args){ | |
//引用普通方法 | |
A a = Calculator::add; | |
System.out.println(a.cal(2,3)); | |
//引用构造方法 | |
StudentFactory stuf = Student::new; | |
Student stu = stuf.getStudent(1,"yewenlin"); | |
System.out.println(stu.id+":"+stu.name); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment