代码如下:
package PrintablePack;public interface Printable {void printString(String s);}
package PrintablePack;public class PrintableDemo {public static void main(String[] args){usePrintable(s-> System.out.println(s));usePrintable(System.out::println);//方法引用符 ::}private static void usePrintable(Printable p){p.printString("hello java");}}
代码如下:
package ConverterPack01;public interface Converter {int convert(String s);
}
package ConverterPack01;public class ConverterDemo {public static void main(String[] args){useConverter(s-> Integer.parseInt(s));useConverter(Integer::parseInt);}private static void useConverter(Converter c){int number = c.convert("666");System.out.println(number);}}
代码如下:
package ConverterPack01;public interface Printer {void printUpperCase(String s);
}
package ConverterPack01;public class PrintString {public void printUpper(String s){String result = s.toUpperCase();System.out.println(result);}}
package ConverterPack01;import java.util.Locale;public class PrinterDemo {public static void main(String[] args){usePrinter(s->
// String result = s.toUpperCase();
// System.out.println(result);System.out.println(s.toUpperCase()));PrintString ps = new PrintString();usePrinter(ps::printUpper);}private static void usePrinter(Printer p){p.printUpperCase("hello world");}}
代码如下:
package ConverterPack01;public interface MyString {String mySubString (String s,int x,int y);
}
package ConverterPack01;public class MyStringDemo {public static void main(String[] args){useMyString((s,x, y)->s.substring(x,y));useMyString(String::substring);}private static void useMyString(MyString my){String s = my.mySubString("helloworld",2,5);System.out.println(s);}
}
代码如下:
package ConverterPack01;public interface StudentBuilder {Student build(String name,int age);
}
package ConverterPack01;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;}
}
package ConverterPack01;public class StudentDemo {public static void main(String[] args){useStudentBuilder((String name,int age)->{
// Student s = new Student(name,age);
// return s;return new Student(name,age);});useStudentBuilder((name,age)->new Student(name,age));useStudentBuilder(Student::new);}private static void useStudentBuilder(StudentBuilder sb){Student s = sb.build("Tom",30);System.out.println(s.getName()+","+s.getAge());}}