lamda表达式

lamda是jdk8的一个新特性
它可以避免匿名内部类定义过多 ,其实质属于函数式编程的概念
它在简单线程的使用比较广泛

首先定义一个接口

interface Myinterface{
	void lambda();
}

用不同的方式去实现这个接口看

使用外部类

class Outer implements Myinterface{
	@Override
	public void lambda() {
		System.out.println("使用外部类");
	}
}

public class Test {
	public static void main(String[] args) {
		   Myinterface  m=new Outer();
		   m.lambda();
	}
}


输出结果:
使用外部类

使用内部类

public class Test {
    static class Inter implements Myinterface{
        @Override
        public void lambda() {
            System.out.println("使用静态内部类");
        }
    }
    public static void main(String[] args) {
        Myinterface  m=new Inter();
        m.lambda();
    }
}

输出结果:
使用静态内部类

使用成员内部类

public class Test {
    public static void main(String[] args) {
        class Inter implements Myinterface{
            @Override
            public void lambda() {
                System.out.println("使用成员内部类");
            }
        }

        Myinterface  m=new Inter();
        m.lambda();
    }
}

输出结果:
使用成员内部类

使用匿名内部类

public class Test {
    public static void main(String[] args) {
        Myinterface  m=new Myinterface() {
            @Override
            public void lambda() {
                System.out.println("使用匿名内部类");
            }
        };
        m.lambda();
    }
}

输出结果:
使用匿名内部类

使用lamda表达式

public class Test {
    public static void main(String[] args) {
        Myinterface  m=()->{
                System.out.println("使用lamda表达式");
        };
        m.lambda();
    }
}
输出结果:
使用lamda表达式

lamda表达式

对于接口只有一个方法,使用lamda表达式可以简化
使用lamda表达式可以扔掉

(params) -> expression  ;    //()表示接口里的那个方法 params表示方法的参数
(params) -> { statements };   //statements就是具体实现接口方法的代码  如果只有一行,可以省略{}括号

1.一个参数

interface Myinterface{
	void lambda(int a);
}
public class Test{
	public static void main(String[] args) {
		Myinterface  m=(int a) -> {
			System.out.println("参数:"+a);
		};
		m.lambda(100);
		
		//简化
		m=(a) -> {    //可以省略类型
				System.out.println("参数:"+a);
		};
		m.lambda(50);
		
		m=a -> {     //一个参数可以省略类型,再省略括号
 			System.out.println("参数:"+a);
		};
		m.lambda(5);
		
		m =a ->System.out.println("参数:"+a);    //只有一句可以省略括号
		m.lambda(0);	
	}
}

输出结果:
参数:100
参数:50
参数:5
参数:0

2.多个参数,带返回值
接口有返回值时

interface Myinterface{
    int lambda(int a,int b);
}
public class Lamda03 {
    public static void main(String[] args) {
        Myinterface m=(int a,int c)->{
            System.out.println("a+c="+(a+c));
                return a+c;
        };
        m.lambda(10,10);

        m=(a,c)->{  //可以省略类型
            System.out.println("a+c="+(a+c));
            return a+c;
        };
        m.lambda(20,20);

        m=(a,c)->{     //只有返回值
            return a+c;
        };
        System.out.println(m.lambda(30,30));

        m=(a,c)->a+c;     //只有返回值,可以省略return ,省略括号{} 
        System.out.println(m.lambda(40,40));
    }
}

输出结果:
a+c=20
a+c=40
60
80
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值