Random random = new Random(); Supplier<Integer> integerSupplier = () -> random.nextInt(100); System.out.println(integerSupplier.get()); System.out.println(integerSupplier.get()); }
Out:
1 2 3
hello world 65 54
使用方法引用实例化Supplier
1 2 3 4 5 6 7 8 9 10 11 12 13 14
publicstaticvoidmain(String[] args){ Supplier<Person> personSupplier = Person::new; Person person1 = personSupplier.get(); person1.setName("张三"); Person person2 = personSupplier.get(); person2.setName("李四"); System.out.println(person1); System.out.println(person2);
Random random = new Random(); Supplier<Integer> randomSupplier = random::nextInt; System.out.println(randomSupplier.get()); System.out.println(randomSupplier.get()); }