java高级开发-方法引用

  1. 引⽤用静态⽅方法:类名称::static ⽅方法名称 ;

  2. 引⽤用某个对象的⽅方法:实例例化对象 :: 普通⽅方法 ;

  3. 引⽤用某个特定类的⽅方法: 类名称 :: 普通⽅方法 ;

  4. 引⽤用构造⽅方法: 类名称 :: 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);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值