package nxr.cn;
//使用匿名内部类可以简化程序
public class LambdaTest {
//静态内部类
static class Like2 implements Ilike{
@Override
public void lambda() {
System.out.println("I am nxr 1");
}
}
public static void main(String[] args) {
//静态内部类实现
Ilike l1 = new Like2();
l1.lambda();
//匿名内部类(省略了类名)
Ilike l2 = new Ilike(){
public void lambda() {
System.out.println("I am nxr 2");
}
};
l2.lambda();
//lambda推导式(进一步省略接口名)
Ilike l3 = ()->{
System.out.println("I am nxr 3");
};
l3.lambda();
//方法内部类
class Like3 implements Ilike{
@Override
public void lambda() {
System.out.println("I am nxr 4");
}
}
Ilike l4 = new Like3();
l4.lambda();
//外部类
Ilike l5 = new Like1();
l5.lambda();
}
}
//接口中只能有一个未实现的方法
interface Ilike{
void lambda();
}
//外部类
class Like1 implements Ilike{
@Override
public void lambda() {
System.out.println("I am nxr 5");
}
}
package nxr.cn;
/**
* lambda推导式+参数
* @author 26651
*
*/
public class match {
public static void main(String[] args) {
//lambda推导式
Animal p = (String name) ->{
System.out.println("你咋那么会吃呢!"+name);
};
p.eat("jsm");
//省略参数的类型
p = (name) ->{
System.out.println("你咋那么会吃呢!"+name);
};
p.eat("yzq");
//省略参数的括号
p = name ->{
System.out.println("你咋那么会吃呢!"+name);
};
p.eat("tjz");
// 当方法体中只有一行代码,省略花括号
p = name -> System.out.println("你咋那么会吃呢!"+name);
p.eat("nxr");
}
}
interface Animal{
void eat(String name);
}
class Pig implements Animal{
@Override
public void eat(String name) {
System.out.println("你咋那么会吃呢!"+name);
}
}