• java8 新特性5 方法引用


    方法引用

    :: 该符号为引用运算符,而它所在的表达式被称为方法引用

    二 案例实操

    2.1 对象实例方法

    引用对象的实例方法,其实就引用类中的成员方法

    • 格式

      对象::成员方法

    1.接口

    public interface Duixiang {
        public String toUpper(String str);
    }
    

    2.定义一个转换大小写的类

    public class Zhuan {
        public String toZhuan(String str){
            return str.toUpperCase();
        }
    }
    

    3.测试结果:

    1. public class TestDuixiang {
    2. public static void main(String[] args) {
    3. //lamda表达式
    4. Duixiang duixiang=(x)->{return x.toUpperCase();};
    5. String k= duixiang.toUpper("ssss");
    6. System.out.println("k:"+k);
    7. //实例2
    8. Zhuan z=new Zhuan();
    9. Duixiang duixiang1=z::toZhuan;
    10. String t=duixiang1.toUpper("abcdd");
    11. System.out.println("t:"+t);
    12. }
    13. }

     2.2  类方法

    1.接口

    public interface H {
        public  int H(String x);
    }

    2.调用

    1. public class TestLei {
    2. public static void main(String[] args) {
    3. H m=(x)->{return Integer.parseInt(x); };
    4. int n=m.H("5");
    5. System.out.println("n:"+n);
    6. //方式2
    7. H h= Integer::parseInt;
    8. int k= h.H("5");
    9. System.out.println("k:"+k);
    10. }
    11. }

    3.调用结果

    2.3  类-实例方法

    引用类的实例方法,其实就是引用类中的成员方法

    格式

    类名::成员方法

    1接口

    public interface LeiMethod {
        public String getSubString(String str,int x,int y);
    }

    2.调用类

    1. public class TestLeiMethod {
    2. public static void main(String[] args) {
    3. //方式1
    4. LeiMethod lm=(String x,int m,int n)->{return x.substring(m,n);};
    5. String s=lm.getSubString("qwertrrereeew",0,4);
    6. //
    7. String ss=get("qwertrrereeew",String::substring);
    8. System.out.println("ss:"+ss);
    9. }
    10. public static String get(String str,LeiMethod lm){
    11. return lm.getSubString(str,0,4);
    12. }
    13. }

    3.结果

     2.4  构造器的使用

    引用构造器,其实就是引用构造方法

    • l格式

      类名::new

    1.接口

    public interface Gou {
        public Teacher getTeacher(String name,int age);
    }

    2.调用

    1. package diaoyong;
    2. /**
    3. * @ClassName: TestTeacher
    4. * @Description: TODO
    5. * @Author: liujianfu
    6. * @Date: 2022/11/06 16:59:51 
    7. * @Version: V1.0
    8. **/
    9. public class TestTeacher {
    10. public static void main(String[] args) {
    11. Gou g=(String name,int age)->{return new Teacher(name,age);};
    12. Teacher t= g.getTeacher("asdfas",45);
    13. System.out.println("name:"+t.getName()+" age"+t.getAge());
    14. //方式2
    15. Teacher tt= get(Teacher::new);
    16. System.out.println("name:"+tt.getName()+" age"+tt.getAge());
    17. //方式3
    18. Gou gg=Teacher::new;
    19. Teacher c= gg.getTeacher("adfa",456);
    20. System.out.println("name:"+c.getName()+" age"+c.getAge());
    21. }
    22. public static Teacher get(Gou g){
    23. Teacher t= g.getTeacher("ass",40);
    24. return t;
    25. }
    26. }

    3.结果

     

  • 相关阅读:
    Mysql——》计算日期差
    什么是grep命令?
    02 nginx 中几种 location 表达式的优先级问题
    supervisor--go版安装
    超全整理——相机标定知识汇总
    Spring bean的生命周期
    lunatic亚毫秒 Web 框架的LiveView实时视图
    phy层深入了解编码
    【数据结构】排序合集(万字详解)
    javascript 基础知识点整理
  • 原文地址:https://blog.csdn.net/u011066470/article/details/127707682