方法引用概述 优化lambda表达式
package com.mujiachao.fangfayinyong;
@FunctionalInterface
public interface PrintTest {
void print1(String s);
}
package com.mujiachao.fangfayinyong;
public class Demo1 {
public static void printTest(PrintTest p){
p.print1("方法引用");
}
public static void main(String[] args) {
printTest(System.out::println);
}
}
通过对象名引用成员方法
package com.mujiachao.fangfayinyong;
@FunctionalInterface
public interface PrintTest {
void print1(String s);
}
package com.mujiachao.fangfayinyong;
public class Method {
public void get(String s){
System.out.println(s.toUpperCase());
}
}
package com.mujiachao.fangfayinyong;
public class Demo2 {
public static void printTest(PrintTest p){
p.print1("mujiachao");
}
public static void main(String[] args) {
Method method = new Method();
printTest(method::get);
}
}
通过类名引用静态成员方法
package com.mujiachao.fangfayinyong;
@FunctionalInterface
public interface PrintTest1 {
int print2(int s);
}
package com.mujiachao.fangfayinyong;
public class Demo3 {
public static int printTest(int a,PrintTest1 p) {
return p.print2(a);
}
public static void main(String[] args) {
int i = printTest(-8, Math::abs);
System.out.println(i);
}
}
通过super引用父类的成员方法
package com.mujiachao.fangfayinyong;
@FunctionalInterface
public interface Greetable {
void greet();
}
package com.mujiachao.fangfayinyong;
public class SuperMan {
public void say(){
System.out.println("我的名字是 :superm");
}
}
package com.mujiachao.fangfayinyong;
public class Man extends SuperMan{
@Override
public void say() {
System.out.println("我的名字是: man");
}
public void method(Greetable g){
g.greet();
}
public void show(){
method(super::say);
}
public static void main(String[] args) {
new Man().say();
new Man().show();
}
}
通过this关键字引用本类的成员方法
package com.mujiachao.fangfayinyong;
public class Man extends SuperMan{
@Override
public void say() {
System.out.println("我的名字是: man");
}
public void method(Greetable g){
g.greet();
}
public void show(){
method(super::say);
}
public static void main(String[] args) {
new Man().say();
new Man().show();
}
}
package com.mujiachao.fangfayinyong;
public class Husband {
public void buyFang(){
System.out.println("买了一辆宝马车");
}
public void buyToo(Buyable b){
b.buy();
}
public void maary(){
buyToo(this::buyFang);
}
public static void main(String[] args) {
new Husband().maary();
}
}
类的构造器引用
package com.mujiachao.fangfayinyong;
public class Person {
private String name;
public Person() {
}
public Person(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.mujiachao.fangfayinyong;
@FunctionalInterface
public interface PersonBuilder {
Person getPerson(String name);
}
package com.mujiachao.fangfayinyong;
public class TestPerson {
public static void getP(String name,PersonBuilder p){
Person person = p.getPerson(name);
System.out.println(person.getName());
}
public static void main(String[] args) {
getP("捷克隽逸",name -> new Person(name));
getP("古力娜扎",Person::new);
}
}
数组的构造器引用
package com.mujiachao.fangfayinyong;
public interface ArrayBuilder {
int[] getArray(int len);
}
package com.mujiachao.fangfayinyong;
public class DemoArray {
public static void gerArrayLength(int len,ArrayBuilder arrayBuilder){
int[] array = arrayBuilder.getArray(len);
System.out.println(array.length);
}
public static void main(String[] args) {
gerArrayLength(201,int[]::new);
}
}