package top.xxx.test1;
public class TestDemo {
public static void main(String[] args){
useStringHandler(new StringHandler(){
@Override
public void printMessage(String message){
System.out.println("我是匿名内部类的"+message+"");}});
useStringHandler((String message)->{
System.out.println("我是lambda表达式的"+message+"");});}
public static void useStringHandler(StringHandler stringHandler){
stringHandler.printMessage("哈哈哈");}}
interface StringHandler{
public void printMessage(String message);}
Lambda表达式练习-无参数有返回值
package top.djzxdx.test1;import java.util.Random;
public class TestDemo {
public static void main(String[] args){
useRandomNumHandler(new RandomNumHandler(){
@Override
public int getNumber(){
Random r = new Random();return r.nextInt(10);}});
useRandomNumHandler(() ->{
Random r = new Random();return r.nextInt(100);});}
public static void useRandomNumHandler(RandomNumHandler randomNumHandler){
int result = randomNumHandler.getNumber();
System.out.println(result);}}
interface RandomNumHandler{
public int getNumber();}
Lambda表达式练习-有参数有返回值
package top.xxx.test1;
public class TestDemo {
public static void main(String[] args){
useCalculator(new Calculator(){
@Override
public int calc(int a, int b){
int result = a + b;return result;}});
useCalculator((int a , int b ) ->{return a + b;});}
public static void useCalculator(Calculator calculator){
int calcResult = calculator.calc(10, 20);
System.out.println(calcResult);}}
interface Calculator {
public int calc(int a, int b);}