[Java] and references cited constructor method

Static method references

Static method referenced format:

方法中(类名::静态方法名称);
method(Math::abs);

等效于重写了calc方法
pulic int calc(int num) {
Math.abs(num);
}

Absolute value:

使用Lambda表达式来实现
method(-10, (int num)->{return Math.abs(num);});

使用方法引用来实现
method(-20,Math::abs);

The method of the object name references

Case conversion:

使用Lambda表达式来实现
method("abc", (String s)->{su.printUpperCase(s);});

使用对象名引用方法来实现
method("abc", su::printUpperCase);

Constructor reference

Constructor citation format:

方法(类名::new);
method(Person::new);

Output name:

使用Lambda表达式来实现
method("杨过", (String name)->{return new Person(name);});

使用构造方法引用来实现
method("小龙女", Person::new);

A class name reference common method

A class name reference format common methods:

方法(类名::普通方法名称);
method(StringUtils::printUpperCase);

Case conversion:

使用Lambda表达式来实现
method(new StringUtils(), "abc", (StringUtils su, String s)->{su.printUpperCase(s);});

使用类名引用普通方法来实现
method(new StringUtils(), "def", StringUtils::printUpperCase);
Published 38 original articles · won praise 4 · Views 809

Guess you like

Origin blog.csdn.net/Hide111/article/details/105183451