方法引用和构造器引用

77 阅读1分钟

方法引用和构造器引用

1、引用类方法

@FunctionalInterface
interface Converter{
    Integer convert(String from);
}

Converter converter1 = from -> Integer.valueOf(from)
    
Converter converter1 = Integer::valueOf;

2、引用特定对象的实例方法

Converter converter2 = from -> "fkit.org".indexOf(from);

Converter converter1 = "fkit.org"::valueOf;

3、引用某类对象的实例方法

@FunctionalInterface
interface MyTest{
    String test(String a, int b, int c);
}

MyTest mt = (a,b,c) -> a.substring(b,c);

MyTest mt = String::substring;

4、引用构造器

@FunctionalInterface
interface YourTest{
    JFrame win(String title);
}

YourTest yt = (String a) -> new JFrame(a)
    
YourTest yt = JFrame::new