从最初开始,只要是进行引用都是针对于引用类型完成的,也就是只有数组、类、接⼝具备引用操作。
⽅法引⽤⼀般结合函数式编程使⽤
引⽤静态⽅法
String类的valueOf()方法
IUtil1 函数接口 (函数): Integer -> String
String.valueOf(x) : 面向对象内容, String类的静态方法
IUtil1 -> Lambda表达式实现函数的具体逻辑
IUtil1函数能够复用面向对象中已有的逻辑
package com.changehyh.github.function;
public class TestStaticMethodRef {
public static void main(String[] args) {
// IUtil<Integer,String> iUtil = (p)->{
// //p 计算+字符拼接
// return String.valueOf(p);
// };
//iUtil1变量此时就是String.valueOf这个方法的别名
// y = f(x)
// f -> { }
// f = String::valueOf
// f(x)
IUtil<Integer,String> iUtil1 = String::valueOf;
System.out.println(iUtil1.convert(10));
}
}
@FunctionalInterface
interface IUtil<P,R>{
//将参数类型 Integer的参数转换String
//y = f(x) x:Integer y:String
//f:转换逻辑
R convert(P p);
}
引⽤对象方法:
String中的toUpperCase()方法为对象方法
package com.changehyh.github.function;
//引用对像方法
public class TestMemberMethodRef {
public static void main(String[] args) {
// IUtil2<String> iUtil2 = new IUtil2<String>() {
// @Override
// public String switchPara() {
// return "hello world".toUpperCase();
// }
// };
// System.out.println(iUtil2.switchPara());
//String 成员方法 toUpperCase()
IUtil2<String> iUtil21 = "hello world"::toUpperCase;
System.out.println(iUtil21.switchPara());
}
}
@FunctionalInterface
interface IUtil2<R>{
R switchPara();
}
引⽤类中普通⽅法:
String有⼀个compareTo⽅法,此⽅法为普通⽅法
package com.changehyh.github.function;
// 引⽤类中普通⽅法
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;
};
System.out.println(iUtil3.compare(20,10));
//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(20,10));
}
}
@FunctionalInterface
interface IUtil3 <P, R>{
//比较类型为P的参数p1和p2 返回R
//y = f(m, n) [m n 同一类型]
R compare(P p1,P p2);
}
引⽤构造⽅法:
package com.changehyh.github.function;
public class TestConstructorMethodRef {
public static void main(String[] args) {
Person person1 = new Person();
System.out.println(person1);
Person person2 = new Person(22,"hyh");
System.out.println(person2);
// 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");
System.out.println(person3);
//构造方法引用
ObjectFactory<Integer,String,Person> factory1 = Person::new;
Person person4 = factory1.createObject(20,"TOM");
System.out.println(person4);
//方法引用 :打通函数实现和面向对象的方法
}
}
class Person{
private Integer age;
private String name;
public Person(){
}
public Person(Integer age,String name){
this.age = age;
this.name = name;
}
public void setAge(Integer age) {
this.age = age;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public String getName() {
return name;
}
@Override
public String toString() {
return "Person{" +
"age=" + age +
", name='" + name + '\'' +
'}';
}
}
@FunctionalInterface
interface ObjectFactory<P1,P2,R>{
R createObject(P1 p1,P2 p2);
}
内建函数式接⼝
package com.changehyh.github.function;
import java.util.Random;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
public class TestBuildInFunction {
/**
y = f(x) : 给入参 x 经过处理 返回结果 y
y = f() : 函数提过一个结果 y
y(void) = f(x) : 给入参 x 处理它
y(boolean) = f(x) : 给入参x 经过处理 返回boolean类的结果 (true false)
*/
//功能型接⼝
// y = f(x) : 给入参 x 经过处理 返回结果 y
public static void function(){
// Function<Integer,String> function = (x)->{
// return String.valueOf(x);
// };
Function<Integer,String> function = String::valueOf;
System.out.println(function.apply(10));
//Lambda
print((p)->{
System.out.println("xxx");
return String.valueOf(p);
},20);
}
// 供给型接⼝
//y = f() : 函数提过一个结果 y
public static void supplier(){
Supplier<String> supplier = ()->{
return "Hello World";
};
System.out.println(supplier.get());
//T 的 get();
// Object();-> object
print(Object::new);
}
//消费型接⼝
public static void consumer(){
Consumer<String> consumer = (x)->{
System.out.println(x);
};
consumer.accept("Hello");
consumer = System.out::println;
}
//断言型接口
public static void predicate(){
Predicate<String> predicate = (x) ->{
if (x==null){
return false;
}else {
return x.length()> 2;
}
};
System.out.println(predicate.test("hello"));
System.out.println(predicate.test(" "));
}
public static void main(String[] args) {
//y = f()
Supplier<String> supplier = ()->{
Random random = new Random();
return String.valueOf(random.nextInt(200));
};
//z = g(x)
Predicate<Supplier<String>> predicate = (x) ->
{
String value = x.get();
System.out.println(value);
if (value==null){
return false;
}else {
return value.length()>2;
}
};
//z = g(f())
boolean rs = predicate.test(supplier);
System.out.println(rs);
}
public static void print (Function<Integer,String> function,Integer p){
System.out.println(function.apply(p));
}
public static void print(Supplier<?> supplier){
System.out.println(supplier.get());
}
}