当前位置: 首页 > news >正文

广州市建设网站互联网行业前沿资讯

广州市建设网站,互联网行业前沿资讯,网络网站制作,互诺 网站好吗一.方法引用 1.概念#xff1a;把已经有的方法拿过来用#xff0c;当作函数式接口中抽象方法的方法体 2.使用方法引用之前需要满足的规则 #xff08;1#xff09;.引用处必须是函数式接口 #xff08;2#xff09;.被引用的方法必须已经存在#xff08;如果不存在就…一.方法引用 1.概念把已经有的方法拿过来用当作函数式接口中抽象方法的方法体 2.使用方法引用之前需要满足的规则 1.引用处必须是函数式接口 2.被引用的方法必须已经存在如果不存在就需要自己写 3.被引用方法的形参和返回值需要跟抽象方法保持一致 4.被引用方法的功能要满足当前需求 例题 创建一个数组进行倒序排列 import java.util.Arrays; import java.util.Comparator;public class FunctionReference {public static void main(String[] args) {Integer[] arr {7, 5, 3, 6, 9, 1, 4, 8, 2};//匿名内部类Arrays.sort(arr, new ComparatorInteger() {Overridepublic int compare(Integer o1, Integer o2) {return o2 - o1;}});System.out.println(Arrays.toString(arr));//Lambda表达式Arrays.sort(arr, (Integer o1, Integer o2) - {return o2 - o1;});System.out.println(Arrays.toString(arr));//方法引用Arrays.sort(arr, FunctionReference::substraction);System.out.println(Arrays.toString(arr));}private static int substraction(Integer o1, Integer o2) {return o2 - o1;} }::是方法引用符  二.方法引用的分类 1.引用静态方法 格式类名::静态方法 范例Integer::parseInt 练习 集合中有以下数字要求把它们都变成int类型 1,2,3,4,5 import java.util.ArrayList; import java.util.Collections;public class FunctionReference {public static void main(String[] args) {ArrayListString str new ArrayList();Collections.addAll(str, 1, 2, 3, 4, 5);ArrayListInteger number new ArrayList();str.stream().map(Integer::parseInt).forEach(integer - System.out.println(integer));} }2.引用成员方法 格式 对象::成员方法 1.引用其他类的成员方法 引用其他类的成员方法的格式其他类对象::方法名 import java.util.ArrayList; import java.util.Collections;public class FunctionReference {public static void main(String[] args) {ArrayListString al new ArrayList();Collections.addAll(al, 张无忌, 周芷若, 赵敏, 张强, 张三丰);al.stream().filter(StringFunction::judge).forEach(s - System.out.println(s));} }StringFunction public class StringFunction {public static boolean judge(String s) {return s.startsWith(张) s.length() 3;} }2.引用本类的成员方法 引用本类的成员方法的格式本类this::方法名 练习1 集合中有一些名字按照要求过滤数据 数据张无忌,周芷若,赵敏,张强,张三丰 要求只要以张开头且名字长度是3的数据 import java.util.ArrayList; import java.util.Collections;public class FunctionReference {public static void main(String[] args) {ArrayListString al new ArrayList();Collections.addAll(al, 张无忌, 周芷若, 赵敏, 张强, 张三丰);//注意静态方法中是没有this的所以这里的FunctionReference即类名不能换做this否则会报错al.stream().filter(FunctionReference::judge).forEach(s - System.out.println(s));}public static boolean judge(String s) {return s.startsWith(张) s.length() 3;} }练习2 GUI界面中点击事件的方法引用写法 Frame import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent;public class Frame extends JFrame {JButton GO new JButton(GO);public Frame() {initFram();initView();this.setVisible(true);}private void initFram() {this.setTitle(Frame);this.setSize(400, 500);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setResizable(false);this.setLocationRelativeTo(null);this.setLayout(null);this.getContentPane().setBackground(Color.WHITE);this.setAlwaysOnTop(true);}private void initView() {GO.setFont(new Font(null, 1, 20));GO.setBounds(120, 150, 150, 50);GO.setBackground(Color.WHITE);GO.addActionListener(this::click);this.getContentPane().add(GO);}public void click(ActionEvent e) {Object o e.getSource();if (o GO) {System.out.println(GO);}} }public class FunctionReference {public static void main(String[] args) {new Frame();} }3.引用父类的成员方法 引用父类的成员方法的格式父类super::方法名 练习2 GUI界面中点击事件的方法引用写法 Frame import javax.swing.*; import java.awt.*;public class Frame extends FatherFrame {JButton GO new JButton(GO);public Frame() {initFram();initView();this.setVisible(true);}private void initFram() {this.setTitle(Frame);this.setSize(400, 500);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setResizable(false);this.setLocationRelativeTo(null);this.setLayout(null);this.getContentPane().setBackground(Color.WHITE);this.setAlwaysOnTop(true);}private void initView() {GO.setFont(new Font(null, 1, 20));GO.setBounds(120, 150, 150, 50);GO.setBackground(Color.WHITE);GO.addActionListener(super::click);this.getContentPane().add(GO);} }FatherFrame import javax.swing.*; import java.awt.event.ActionEvent;public class FatherFrame extends JFrame {public void click(ActionEvent e) {System.out.println(GO);} }public class FunctionReference {public static void main(String[] args) {new Frame();} }注意在引用父类和本类的成员方法且使用this/super的格式时引用处不能是静态方法因为静态方法中没有this和super关键字会报错 3.引用构造方法 格式类名::new 范例Student::new 练习 集合里面存储姓名和年龄比如A,18 要求将数据封装成Student对象并收集到List集合中 Student里的属性String nameint age import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.stream.Collectors;public class FunctionReference {public static void main(String[] args) {ArrayListString al new ArrayList();Collections.addAll(al, A,18, B,19, C,20, D,21, E,22);/*没有引用构造方法ListStudent collect al.stream().map(new FunctionString, Student() {Overridepublic Student apply(String s) {String[] str s.split(,);String name str[0];int age Integer.parseInt(str[1]);return new Student(name, age);}}).collect(Collectors.toList());System.out.println(collect);*///引用构造方法ListStudent collect al.stream()//这里记得回到Student类中写一个只有一个参数的构造方法以匹配引用成员方法的规则否则在下方会报错.map(Student::new).collect(Collectors.toList());System.out.println(collect);} }4.其他引用方式 1.使用类名引用成员方法 格式类名::成员方法 范例String::substring 练习 集合里面存有一些字符串要求变成大写后进行输出 import java.util.ArrayList; import java.util.Collections; import java.util.function.Function;public class FunctionReference {public static void main(String[] args) {ArrayListString al new ArrayList();Collections.addAll(al, aaa, bbb, ccc, ddd, eee);/*没有进行引用al.stream().map(new FunctionString, String() {Overridepublic String apply(String s) {return s.toUpperCase();}}).forEach(s - System.out.println(s));*/al.stream().map(String::toUpperCase).forEach(s - System.out.println(s));} }2.引用数组的构造方法 格式数据类型[]::new 范例int[]::new 练习 集合中存储了一些整形数据将它们收集到数组当中 import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.function.IntFunction;public class FunctionReference {public static void main(String[] args) {ArrayListInteger al new ArrayList();Collections.addAll(al, 1, 2, 3, 4, 5, 6, 7, 8, 9);/*没有进行引用Integer[] arr al.stream().toArray(new IntFunctionInteger[]() {Overridepublic Integer[] apply(int value) {return new Integer[value];}});System.out.println(Arrays.toString(arr));*/Integer[] arral.stream().toArray(Integer[]::new);System.out.println(Arrays.toString(arr));} }
http://www.zqtcl.cn/news/460492/

相关文章:

  • 网站内容规范外贸电商怎么做
  • 郑州做网站齿轮wordpress 文章h标签美化
  • 建设银行网站怎么修改手机号码吗网站建设怎样容易
  • 网站建设风险管理计划书户外媒体网站建设免费
  • 学到什么程度可以做网站网站维护的要求包括
  • 泉州网站设计平台南阳响应式网站
  • 阿里云 企业网站选哪种推广普通话的文字内容
  • 广州市南沙建设局网站中山建网站咨询电话
  • 怎么创建网站快捷方式网络服务器搭建配置与管理 下载
  • 现在什么类型网站没有人做wordpress get_categories()
  • 石家庄网站推广优化闲鱼网络营销方式
  • 精诚时代 网站谁做的北京网站设计必看刻
  • 长沙网站排名报价企业管理培训课程网课
  • 怎样做婚庆网站外贸网站怎么注册
  • 网站设计制作公司推荐自己做商务网站有什么利弊
  • 传媒网站杨浦网站建设哪家好
  • 500m主机空间能做视频网站吗做中文网站的公司
  • 网站建设规划模板公司网站建设论文
  • p2p网站开发的内容广东世纪达建设集团有限公司官方网站
  • 网站基本建设是什么o元做网站
  • 南昌做购物网站的公司mc做弊端网站
  • 汕头制作网站推荐文化建设五大工程
  • 公司购物网站备案遵义市乡村街道建设投诉网站
  • ps做景观有哪些素材网站网站推广软文
  • 医疗类网站备案dw网页设计期末作业源代码
  • 网站建设开发合同别具光芒 Flash互动网站设计
  • app导航网站建设多少钱网页游戏破解版
  • 布吉做棋牌网站建设哪家服务好青海做高端网站建设的公司
  • 邙山郑州网站建设好看手机网站推荐
  • 北京建设网官方网站外贸wordpress收款插件