-
引⽤用静态⽅方法:类名称::static ⽅方法名称 ;
-
引⽤用某个对象的⽅方法:实例例化对象 :: 普通⽅方法 ;
-
引⽤用某个特定类的⽅方法: 类名称 :: 普通⽅方法 ;
-
引⽤用构造⽅方法: 类名称 :: new 。
IUtil1 函数接口 (函数): Integer -> String
String.valueOf(x) : 面向对象内容, String类的静态方法
IUtil1 -> Lambda表达式实现函数的具体逻辑
IUtil1函数能够复用面向对象中已有的逻辑
1丶引用静态方法
String类的valueOf()方法
public class TestStaticMethodRef {
public static void main(String[] args) {
/**
* IUtil1 函数接口 (函数): Integer -> String
*
* String.valueOf(x) : 面向对象内容, String类的静态方法
*
* IUtil1 -> Lambda表达式实现函数的具体逻辑
*
* IUtil1函数能够复用面向对象中已有的逻辑
*
*/
// IUtil1<Integer, String> iUtil1 = (p) -> {
// //p 计算+字符拼接 => String
// return String.valueOf(p);
// };
//iUtil1变量此时就是String.valueOf这个方法的别名
// y = f(x)
// f -> { }
// f = String::valueOf
// f(x)
IUtil1<Integer,String> iUtil1 = String::valueOf;
System.out.println(iUtil1.convert(10));
}
}
@FunctionalInterface
interface IUtil1<P, R> {
/**
* 将参数类型为Integer的参数转换为String
* <p>
* y = f(x)
* x : Integer
* y : String
* f : 装换逻辑(数学公式)
*
* @return
*/
// String convert(Integer value);
/**
* y = f(x)
* x : P
* y : R
* f : 转换逻辑(数学公式)
*
* @param p
* @return
*/
R convert(P p);
}
2丶引用对象方法
String中的toUpperCase()⽅方法为对象方法
public class TestMemberMethodRef {
public static void main(String[] args) {
IUtil2<String> iUtil2 = new IUtil2<String>() {
@Override
public String switchParam() {
return "Hello".toUpperCase();
}
};
System.out.println(iUtil2.switchParam());
//String 成员方法 toUpperCase()
IUtil2<String> iUtil21 = "Hello"::toUpperCase;
System.out.println(iUtil21.switchParam());
}
}
@FunctionalInterface
interface IUtil2<R>{
R switchParam();
}
3丶引用类中普通方法
String有⼀一个compareTo⽅方法,此⽅方法为普通⽅方法
public class TestClassMemberMethodRef {
public static void main(String[] args) {
//p1 > p2 return >0
//p1 = p2 return 0
//p1 < p2 return <0
IUtil3<Integer,Integer> iUtil3 = (p1,p2) -> {
return p1 - p2;
};
//int -> Integer
//Integer Integer(object).compareTo(Integer)
//Integer compare(Integer,Integer)
//Integer a = new Integer(10);
// a.compareTo(new Integer(12));
//通过类型::成员方法进行方法引用
IUtil3<Integer,Integer> iUtil31 = Integer::compareTo;
System.out.println(iUtil31.compare(10,20));
}
}
@FunctionalInterface
interface IUtil3<P,R>{
R compare(P p1,P p2);
}
4丶引用构造方法
public class TestConstructorMethodRef {
public static void main(String[] args) {
Person person1 = new Person();
Person person2 = new Person(22,"jack");
// y = f(m, n)
// y = Person类型
// m = Integer类型
// n = String类型
// f = 根据m n 创建一个Person
//new Person()
//new Person(Integer,String)
ObjectFactory<Integer,String,Person> factory = (p1,p2) ->{
return new Person(p1,p2);
};
Person person3 = factory.createObject(20,"Alice");
//构造方法引用
ObjectFactory<Integer,String,Person> factory1 = Person::new;
Person person4 = factory.createObject(21,"Tom");
//方法引用:打通函数实现和面向对象的方法
}
}
class Person{
private Integer age;
private String name;
public Person() {
}
public Person(Integer age,String name){
this.age = age;
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Person{" +
"age=" + age +
", name='" + name + '\'' +
'}';
}
}
@FunctionalInterface
interface ObjectFactory<P1,P2,R> {
R createObject(P1 p1,P2 p2);
}