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

赣icp上饶网站建设河南旅游网页设计

赣icp上饶网站建设,河南旅游网页设计,房子装修改造,wordpress不兼容ie8Lambda表达式用法汇总 java8 中引入的 Lambda 表达式真的是个好东西#xff0c;掌握之后#xff0c;写代码更简洁了#xff0c;码字效率也提升了不少#xff0c;这里咱 们一起来看看 Lambada 表达式常见的写法#xff0c;加深理解。 1、有参无返回值函数式接口 8 种写法…Lambda表达式用法汇总 java8 中引入的 Lambda 表达式真的是个好东西掌握之后写代码更简洁了码字效率也提升了不少这里咱 们一起来看看 Lambada 表达式常见的写法加深理解。 1、有参无返回值函数式接口 8 种写法 1.1、先来定义一个函数式接口 有 2 个参数无返回值 FunctionalInterface public interface Fun {void print(String s1, String s2); }创建 Fun 有 8 种写法。 1.2、第 1 种原始的写法如下 Fun fun1 new Fun() {Overridepublic void print(String s1, String s2) {String msg s1 , s2;System.out.println(msg);} };1.3、第 2 种Lambda 方式完整写法 Fun fun2 (String s1,String s2) - {String msg s1 , s2;System.out.println(msg); };1.4、第 3 种Lambda 方式隐藏参数类型 Fun fun3 (s1, s2) - {String msg s1 , s2;System.out.println(msg); };1.5、第 4 种Lambda 方式单行可去掉{} Fun fun4 (s1, s2) - System.out.println(s1 , s2);1.6、第 5 种Lambda 方式引用静态方法 添加一个静态方法 public static class StaticMethod {public static void print(String s1, String s2) {String msg s1 , s2;System.out.println(msg);} }继续简写 Fun如下 Fun fun5 (s1, s2) - StaticMethod.print(s1,s2);1.7、第 6 种Lambda 方式使用::引用静态方法隐藏参数 若静态方法的参数和函数式接口的方法参数一致可以对下面代码继续优化 Fun fun5 (s1, s2) - StaticMethod.print(s1,s2);可以优化为下面这样 Fun fun6 StaticMethod::print;1.8、第 7 种Lambda 方式引用实例方法 添加一个实例对象内部添加一个实例方法 public static class InstanceMethod {public void print(String s1, String s2) {String msg s1 , s2;System.out.println(msg);} }继续简写 Fun如下 InstanceMethod instanceMethod new InstanceMethod(); Fun fun7 (s1, s2) - instanceMethod.print(s1,s2);1.9、第 8 种Lambda 方式使用::实例态方法隐藏参数 若实例方法的参数和函数式接口的方法参数一致可以对下面代码继续优化 Fun fun7 (s1, s2) - instanceMethod.print(s1,s2);优化为下面这样 Fun fun8 instanceMethod::print;2、无参有返回值函数式接口 8 种写法 2.1、来一个有返回值的函数式接口 函数式接口 UserFun 中有个 createUser 方法用来创建 User 对象User 类有 2 个属性定义了 2 个构造方法StaticMethod 提供了一个静态方法 user()用来创建 User 对象InstanceMethod 提供了一个实例方法 user()用来创建 User 对象 FunctionalInterface public interface UserFun {User createUser(); }public static class User {private String name;private Integer age;public User() {}public User(String name, Integer age) {this.name name;this.age age;} } public static class StaticMethod {public static User user() {return new User(张三, 30);} }public static class InstanceMethod {public User user() {return new User(张三, 30);} }创建 UserFun 有 8 种方式下面来看各种方式演化的过程。 2.2、创建 UserFun 的 8 种写法 Test public void test1() {//方式1原始方式UserFun fun1 new UserFun() {Overridepublic User createUser() {return new User(张三, 30);}};//方式2Lambda玩转方式()-{方法体;}UserFun fu2 () - {return new User(张三, 30);};//方式3方法体只有一行可去掉{}UserFun fun3 () - new User(张三, 30);//方式4调用静态方法UserFun fun4 () - {return StaticMethod.user();};//方式5去掉{}UserFun fun5 () - StaticMethod.user();//方式6使用::调用静态方法UserFun fun6 StaticMethod::user;InstanceMethod instanceMethod new InstanceMethod();//方式7实例方法UserFun fun7 () - instanceMethod.user();//方式8实例对象::实例方法UserFun fun8 instanceMethod::user; }2.3、第 9 种方式类::new 的 代码如下下面 2 种创建方式类似第一种是第二种的简写。 //方式9类::new的方式 UserFun fun9 User::new; //这个相当于 UserFun fun10 new UserFun(){Overridepublic User createUser() {return new User();} };3、有参有返回值的函数式接口 3.1、来一个有参、有返回值的函数式接口 函数式接口 UserFun 中有个有参的 createUser(String name,Integer age)方法用来创建 User 对象User 类有 2 个属性定义了 1 个有参构造方法StaticMethod 提供了一个静态方法 user(String name,Integer age)用来创建 User 对象InstanceMethod 提供了一个实例方法 user(String name,Integer age)用来创建 User 对象 FunctionalInterface public interface UserFun {User createUser(String name, Integer age); }public static class User {private String name;private Integer age;public User(String name, Integer age) {this.name name;this.age age;} }public static class StaticMethod {public static User user(String name, Integer age) {return new User(name, age);} }public static class InstanceMethod {public User user(String name, Integer age) {return new User(name, age);} }3.2、创建 UserFun 的 11 种写法 //方式1原始方式 UserFun fun1 new UserFun() {Overridepublic User createUser(String name, Integer age) {return new User(name, age);} };//方式2Lambda玩转方式()-{方法体;} UserFun fun2 (String name, Integer age) - {return new User(name, age); };//方式3方法体只有一行可去掉{} UserFun fun3 (String name, Integer age) - new User(张三, 30);//方式4隐藏参数类型 UserFun fun4 (name, age) - new User(张三, 30);//方式5调用静态方法 UserFun fun5 (String name, Integer age) - {return StaticMethod.user(name, age); };//方式6隐藏参数类型单行可以去掉{}和return UserFun fun6 (name, age) - StaticMethod.user(name, age);//方式7使用::调用静态方法参数类型数量需要和静态方法一致 UserFun fun7 StaticMethod::user;InstanceMethod instanceMethod new InstanceMethod(); //方式8调用实例方法 UserFun fun8 (String name, Integer age) - {return instanceMethod.user(name, age); };//方式9隐藏参数类型单行可以去掉{}和return UserFun fun9 (name, age) - instanceMethod.user(name, age);//方式10使用::调用静态方法参数类型数量需要和实例方法一致 UserFun fun10 instanceMethod::user;//方式11类::new的方式函数接口中的方法参数需要和User类中构造方法的参数类型数量一致 UserFun fun11 User::new; //这个相当于 UserFun fun12 new UserFun(){Overridepublic User createUser(String name,Integer age) {return new User(name,age);} };4、无参无返回值 Runnable a1 () - System.out.println(Hello); new Thread(a1).start(); a1.run();5、特殊案例 下面这个案例比较特殊下面列出了 5 种写法这几种写法效果是一样的。 方式 5 比较特殊注意看注释大家领会领会。 public class LambdaTest4 {FunctionalInterfacepublic interface Fun {String dispose(String s1, String s2);}Testpublic void test1() {//方式1原始写法Fun fun1 new Fun() {Overridepublic String dispose(String s1, String s2) {return s1.concat(s2);}};//方式2lambda玩转写法Fun fun2 (String s1, String s2) - {return s1.concat(s2);};//方式3单行可以隐藏{}和returnFun fun3 (String s1, String s2) - s1.concat(s2);//方式4隐藏参数Fun fun4 (s1, s2) - s1.concat(s2);/*** 方式5类::方法这种写法需要满足* 1、函数接口方法的第1个参数类型需要和{类名::方法}中的类名是同种类型* 2、第1个参数后面的参数将作为{类名::方法}中方法的参数注意下面的concat方法只有1个参数*/Fun fun5 String::concat;} }6、使用 6.1、有参无返回值函数式接口 package com.example.lambdatest;/*** author zhangshixing* 有参无返回值函数式接口*/ public class Test1 {public static void main(String[] args) {Fun fun (String s1, String s2) - {String msg s1 , s2;System.out.println(msg);};// hello,worldfun.print(hello, world);}FunctionalInterfacepublic interface Fun {void print(String s1, String s2);} }6.2、无参有返回值函数式接口 package com.example.lambdatest;/*** author zhangshixing* 无参有返回值函数式接口*/ public class Test2 {public static void main(String[] args) {Fun fun () - Hello,World;String returnStr fun.getNumber();System.out.println(returnStr);}FunctionalInterfacepublic interface Fun {String getNumber();} }6.3、有参有返回值的函数式接口 package com.example.lambdatest;/*** author zhangshixing* 有参有返回值的函数式接口*/ public class Test3 {public static void main(String[] args) {Fun fun (a, b) - a b;int result fun.add(1,2);System.out.println(result);}FunctionalInterfacepublic interface Fun {int add(int a, int b);} }6.4 无参无返回值 Runnable a1 () - System.out.println(Hello); new Thread(a1).start(); a1.run();
http://www.pierceye.com/news/839926/

相关文章:

  • 做网站用什么面板好网站建设网站公司
  • 寻求网站建设技术网页升级访问永久你懂的
  • 做网站的公司有多少家无后台基础怎么建设网站
  • 在公司做网站是什么职位有链接的网站怎么做
  • 手机网站开发前台架构专业群建设网站
  • 做网站设计怎么样网站ui怎么做的
  • 企业网站用织梦好吗ui培训的课程都有哪些
  • 临沂专业网站建设公司哪家好做网站的照片要多大像素
  • 山东滕州做网站技术电话wordpress网页登陆
  • 做公司网站的费用flash交互网站页面切换制作
  • 网络推广渠道有哪些百度手机seo
  • 重庆专业网站建设公司哪家好seo的中文意思是什么
  • 做品牌折扣微信推广的网站网站换主机换域名
  • 营销型网站有哪些建设流程怎样制作免费的网站
  • 天津建设工程计价网站手工加工网
  • 温州做美食网站网站建设的方案模板下载
  • 如何快速网站备案以用户为中心 建设学校网站
  • 宣传型网站有哪些宁波建设信息港网站
  • php网站开发是做什么的phpcms v9企业网站模板(简洁利于优化)
  • 什么是网站和网页wordpress启用插件出错
  • asp网站制作工具怎么样做国际网站生意
  • 签订网站建设合同山东建设工程招标网官方网站
  • 迅速建设企业网站外贸网站服务器选择
  • 建设网站详细流程wordpress建站数据库
  • 贵阳建立网站聊城网站建设设计
  • 网站怎么设置关键词百度网址大全首页设为首页
  • 中企动力网站怎么样怎么做公司内网网站
  • 求职网站网页模板一个网站可以做多少个小程序
  • 深圳市住房和建设局网站登录怎样在百度建网站
  • 外国做视频在线观看网站asp简单网站开发