Case 1: Lambda expression exercise 1
Requirements: define an interface (Eatable), which defines an abstract method: void eat();
Define a test class (EatableDemo) and provide two methods in the test class
One method is: use eatable (eatable E)
One method is the main method, and the useEatable method is called in the main method.
Interface
package test.test96; // Define an interface (Eatable), which defines an abstract method: void eat(); public interface Eatable { void eat(); }
Interface implementation class
package test.test96; public class EatableImpl implements Eatable{ @Override public void eat() { System.out.println("An apple a day keeps the doctor away"); } }
Test class
package test.test96; /** * Define a test class (EatableDemo) and provide two methods in the test class * One method is: use eatable (eatable E) * One method is the main method, and the useEatable method is called in the main method. */ public class EatableDemo { public static void main(String[] args) { // Calling the useEatable method in the main method Eatable e = new EatableImpl(); useEatable(e); // Anonymous Inner Class useEatable(new Eatable() { @Override public void eat() { System.out.println("An apple a day keeps the doctor away"); } }); // Lambda expression useEatable(() -> { System.out.println("An apple a day keeps the doctor away"); }); } private static void useEatable(Eatable e){ e.eat(); } }
Case 2: Lambda expression exercise 2
Requirement: define an interface (Flyable), which defines an abstract method: void fly(String s);
Define a test class (FlyableDemo) and provide two methods in the test class
One method is: use flyable (flyable f)
One method is the main method, and the useFlyable method is called in the main method.
Interface
package test.test97; // Define an interface (Flyable), which defines an abstract method: void fly(String s); public interface Flyable { void fly(String s); }
Test class
package test.test97; /** * Define a test class (FlyableDemo) and provide two methods in the test class * One method is: use flyable (flyable f) * One method is the main method, and the useFlyable method is called in the main method. */ public class FlyableDemo { public static void main(String[] args) { // Calling the useFlyable method in the main method // Anonymous Inner Class usrFlyable(new Flyable() { @Override public void fly(String s) { System.out.println(s); System.out.println("Plane self driving tour"); } }); // Lambda expression usrFlyable((String s) -> { System.out.println(s); System.out.println("Plane self driving tour"); }); } private static void usrFlyable(Flyable f){ f.fly("The wind is sunny and the sky is clear"); } }
Case 3: Lambda expression exercise 3
Requirement: define an interface (Addable), which defines an abstract method: int add(int x,int y);
Define a test class (AddableDemo) and provide two methods in the test class
One method is: use addable (addable a)
One method is the main method, and the useAddable method is called in the main method.
Interface
package test.test98; // Define an interface (Addable), which defines an abstract method: int add(int x,int y); public interface Addable { int add(int x,int y); }
Test class
package test.test98; /** * Define a test class (AddableDemo) and provide two methods in the test class * One method is: use addable (addable a) * One method is the main method, and the useAddable method is called in the main method. */ public class AddableDemo { public static void main(String[] args) { // Calling the useAddable method in the main method // Lambda expression useAddable((int x, int y) -> { return x + y; }); } private static void useAddable(Addable a){ int sum = a.add(10, 20); System.out.println(sum); } }
Case 4: Reference method
Requirements: define an interface (Converter) in which an abstract method is defined
int convert(String s);
Define a test class (ConverterDemo) and provide two methods in the test class
One method is: useConverter(Converter c)
One method is the main method, and the useConverter method is called in the main method.
Interface
package test.test99; /** * Define an interface (Converter), which defines an abstract method * int convert(String s); */ public interface Converter { int convert(String s); }
Test class
package test.test99; /** * Define a test class (ConverterDemo) and provide two methods in the test class * One method is: useConverter(Converter c) * One method is the main method, and the useConverter method is called in the main method. */ public class ConverterDemo { public static void main(String[] args) { // Calling the useConverter method in the main method // Lambda expression useConverter(s -> Integer.parseInt(s)); // Reference class method useConverter(Integer :: parseInt); // When a Lambda expression is replaced by a class method, all its formal parameters are passed to the static method as parameters } private static void useConverter(Converter c){ int number = c.convert("666"); System.out.println(number); } }
Case 5: instance method of reference object
Requirements: define a class (PrintString) with a method
public void printUpper(String s): change the string parameter to uppercase data, and then output it on the console
Define an interface (Printer), which defines an abstract method
void printUpperCase(String s)
Define a test column (PrinterDemo) and provide two methods in the test class
One method is: usePrinter(Printer p)
One method is the main method, and the usePrinter method is called in the main method.
class
package test.test100; /** * Define a class (PrintString) with a method * public void printUpper(String s): Change the string parameter to uppercase data, and then output it on the console */ public class PrintString { // Change the string parameter to uppercase data, and then output it on the console public void printUpper(String s){ String result = s.toUpperCase(); System.out.println(result); } }
Interface
package test.test100; /** * Define an interface (Printer), which defines an abstract method * void printUpperCase(String s) */ public interface Printer { void printUpperCase(String s); }
Test class
package test.test100; /** * Define a test column (PrinterDemo) and provide two methods in the test class * One method is: usePrinter(Printer p) * One method is the main method, and the usePrinter method is called in the main method. */ public class PrinterDemo { public static void main(String[] args) { // One method is the main method, and the usePrinter method is called in the main method. // Lambda expression usePrinter(s -> System.out.println(s.toUpperCase())); // Instance method of reference object PrintString ps = new PrintString(); usePrinter(ps :: printUpper); //When a Lambda expression is replaced by an instance method of an object, all its formal parameters are passed to the method as parameters } private static void usePrinter(Printer p){ p.printUpperCase("HelloWorld"); } }
Case 6: instance method of reference class
Requirement: define an interface (MyString), which defines an abstract method:
String mySubString(String s,int x,int y);
Define a test class (MyStringDemo) and provide two methods in the test class
One method is: use mystring (mystring my)
One method is the main method, and the useMyString method is called in the main method.
Interface
package test.test101; /** * Define an interface (MyString), which defines an abstract method: * String mySubString(String s,int x,int y); */ public interface MyString { String mySubString(String s,int x,int y); }
Test class
package test.test101; /** * Define a test class (MyStringDemo) and provide two methods in the test class * One method is: use mystring (mystring my) * One method is the main method, and the useMyString method is called in the main method. */ public class MyStringDemo { public static void main(String[] args) { // Calling the useMyString method in the main method // Lambda expression useMyString((s,x,y) -> s.substring(x,y)); // Instance methods in reference classes useMyString(String :: substring); // When a Lambda expression is replaced by an instance method of a class // The first parameter is the caller // All the following parameters are passed to the method as parameters } private static void useMyString(MyString my){ String s = my.mySubString("HelloWorld", 2, 5); System.out.println(s); } }
Case 7: reference constructor
Requirements: define a class (Student) with two member variables (name,age)
It also provides parameterless construction methods and parameterless construction methods, as well as get and set methods corresponding to member variables
Define an interface (StudentBuilder) that defines an abstract method
Student build(String name,int age);
Define a test class (StudentDemo) and provide two methods in the test class
One method is: use studentbuilder (studentbuilder s)
One is the main method, and the useStudentBuilder method is called in the main method.
class
package test.test102; /** * Define a class (Student) with two member variables (name,age) * It also provides parameterless construction methods and parameterless construction methods, as well as get and set methods corresponding to member variables */ public class Student { private String name; private int age; public Student(){ } public Student(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
Interface
package test.test102; /** * Define an interface (StudentBuilder) that defines an abstract method * Student build(String name,int age); */ public interface StudentBuilder { Student build(String name,int age); }
Test class
package test.test102; /** * Define a test class (StudentDemo) and provide two methods in the test class * One method is: use studentbuilder (studentbuilder s) * One is the main method, and the useStudentBuilder method is called in the main method. */ public class StudentDemo { public static void main(String[] args) { // Calling the useStudentBuilder method in the main method // Lambda expression useStudentBuilder(((name, age) -> new Student(name,age))); // Reference constructor useStudentBuilder(Student :: new); // When a Lambda expression is replaced by a constructor, all its formal parameters are passed to the constructor } private static void useStudentBuilder(StudentBuilder sb){ Student s = sb.build("Zhang San", 30); System.out.println(s.getName()+","+s.getAge()); } }