diff --git a/src/class39/LambdaDemo1.java b/src/class39/LambdaDemo1.java new file mode 100644 index 0000000..c43eb6a --- /dev/null +++ b/src/class39/LambdaDemo1.java @@ -0,0 +1,31 @@ +package class39; + +public class LambdaDemo1 { + public static void main(String[] args) { + Animal a = new Animal() { + @Override + public void cry() { + System.out.println("cry..."); + } + }; + a.cry(); +// Swim s1 = new Swim() { +// @Override +// public void swimming() { +// System.out.println("student is swimming..."); +// } +// }; + + Swim s1 = () -> System.out.println("student is swimming...");//lambda表达式,函数式接口的实现 + + } +} + +abstract class Animal{ + public abstract void cry(); +} +//函数式接口:有且只有一个抽象方法 +@FunctionalInterface //声明函数式接口的注解 +interface Swim{ + void swimming(); +} \ No newline at end of file diff --git a/src/class39/LambdaDemo2.java b/src/class39/LambdaDemo2.java new file mode 100644 index 0000000..ab4c8b2 --- /dev/null +++ b/src/class39/LambdaDemo2.java @@ -0,0 +1,42 @@ +package class39; + +import class38.Student; + +import javax.swing.*; +import java.util.Arrays; + +public class LambdaDemo2 { + public static void main(String[] args) { + test2(); + } + public static void test1(){ + Student[] students = new Student[6]; + students[0] = new Student("般素素", 35, 171.5, '女'); + students[1] = new Student("杨幂", 28, 168.5, '女'); + students[2] = new Student("张无忌", 25, 181.5, '男'); + students[3] = new Student("小昭", 19, 165.5, '女'); + students[4] = new Student("赵敏", 27, 167.5, '女'); + students[5] = new Student("刘亦菲", 36, 168, '女'); + //IDEA提示简化 +// Arrays.sort(students, Comparator.comparingInt(Student::getAge)); + Arrays.sort(students, (o1, o2) -> o1.getAge() - o2.getAge()); + + for (Student student : students) { + System.out.println(student); + } + } + public static void test2(){ + JFrame win = new JFrame("登陆窗口"); + win.setSize(300,200); + win.setLocationRelativeTo(null); + win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + win.setVisible(true); + JPanel panel = new JPanel(); + win.add(panel); + JButton btn = new JButton("登陆"); + btn.addActionListener(e -> System.out.println("点击了登陆按钮")); + panel.add(btn); + + + } +}