赞
踩
//创建MyRunnable类的对象 class MyRunnable implements Runnable{ @Override public void run() { System.out.println("多线程程序启动了..."); } } public class LambdaDemo { public static void main(String[] args) { //实现类的方式实现需求 MyRunnable my = new MyRunnable(); //创建Thread类的对象,把MyRunnable的对象作为构造参数传递 Thread t = new Thread(my); //启动线程 t.start(); } }
public class LambdaDemo {
public static void main(String[] args) {
//匿名内部类的方式改进
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("多线程程序启动了...");
}
}).start();
}
}
public class LambdaDemo {
public static void main(String[] args) {
//Lambda表达式改进
new Thread(() -> {
System.out.println("多线程程序启动了...");
}).start();
}
}
System.out.println("多线程程序启动了...");
public interface Eatable {
void eat();
}
class EatableImpl implements Eatable{ @Override public void eat() { System.out.println("一天一苹果,医生远离我"); } } public class EatableDemo { public static void main(String[] args) { //在主方法中调用useEatable方法 Eatable e = new EatableImpl(); useEatable(e); } private static void useEatable(Eatable e){ e.eat(); } }
public class EatableDemo { public static void main(String[] args) { //匿名内部类 useEatable(new Eatable() { @Override public void eat() { System.out.println("一天一苹果,医生远离我"); } }); } private static void useEatable(Eatable e){ e.eat(); } }
public class EatableDemo {
public static void main(String[] args) {
//用Lambda表达式
useEatable(() -> {
System.out.println("一天一苹果,医生远离我");
});
}
private static void useEatable(Eatable e){
e.eat();
}
}
public interface Flyable{
void fly(String s);
}
public class FlyableDemo { public static void main(String[] args) { //在主方法中调用useFlyable方法 //匿名内部类 useFlyable(new Flyable() { @Override public void fly(String s) { System.out.println(s); System.out.println("飞机自驾游"); } }); } private static void useFlyable(Flyable f){ f.fly("风和日丽,晴空万里"); } }
public class FlyableDemo {
public static void main(String[] args) {
//Lambda
useFlyable((String s)-> {
System.out.println(s);
System.out.println("飞机自驾游");
});
}
private static void useFlyable(Flyable f){
f.fly("风和日丽,晴空万里");
}
}
public interface Addable {
int add(int x,int y);
}
public class AddableDemo {
public static void main(String[] args) {
//在主方法中调用useAddle方法
//Lambda
useAddable((int a, int b) -> {
return a+b;
});
}
private static void useAddable(Addable a){
int sum = a.add(10,20);
System.out.println(sum);
}
}
public interface Flyable{
void fly(String s);
}
public interface Addable {
int add(int x,int y);
}
/*Lambda表达式的省略模式*/ public class LambdaDemo { public static void main(String[] args) { //useAddable((int x,int y) -> { // return x+y; //}); //1.参数的类型可以省略,但是有多个参数的情况下,不能只省略一个 useAddable((x,y) -> { return x+y; }); //useFlyable((String s) ->{ // System.out.println(s); //}); //2.如果参数有且只有一个,那么小括号可以省略 useFlyable( s->{ System.out.println(s); }); //3.如果代码块的语句只有一条,可以省略大括号和分号 useFlyable(s -> System.out.println(s)); //4.如果代码块的语句只有一条,可以省略大括号和分号;如果有return,return也要省略 useAddable((x,y) -> x+y ); } private static void useFlyable(Flyable f){ f.fly("风和日丽,晴空万里"); } private static void useAddable(Addable a){ int sum = a.add(10,20); System.out.println(sum); } }
/*Lambda表达式的注意事项*/ public class LambdaDemo { public static void main(String[] args) { //使用Lambda表达式必须要有接口,并且要求接口中有且仅有一个抽相方法 useInter(()->System.out.println("好好学习,天天向上")); //必须有上下文环境,才能推导出Lambda对应的接口 Runnable r = () -> System.out.println("Lambda表达式"); new Thread(r).start(); new Thread( () -> System.out.println("Lambda表达式") ).start(); } private static void useInter(Inter i){ i.show(); } }
abstract class Animal { public abstract void method(); } class Student { public void study(){ System.out.println("爱生活,爱java"); } } public class LambdaDemo{ public static void main(String[] args) { /*//匿名内部类 useInter(new Inter() { @Override public void show() { System.out.println("接口"); } }); useAnimal(new Animal() { @Override public void method() { System.out.println("抽象类"); } }); useStudent(new Student(){ @Override public void study(){ System.out.println("具体类"); } });*/ //Lambda useInter(() -> System.out.println("接口")); //useAnimal(() -> System.out.println("抽象类"));//不可以 //useStudent(() -> System.out.println("具体类"));//不可以 } private static void useStudent(Student s){ s.study(); } private static void useAnimal(Animal a){ a.method(); } private static void useInter(Inter i){ i.show(); } }
public interface Inter{
void show();
void show1();
}
public class LambdaDemo{
public static void main(String[] args) {
useInter(new Inter() {
@Override
public void show() {
System.out.println("show");//声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/505670
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。